bug fix when stopping threads