fixed some thread safety bugs