fix terminate's premature death and add threading