fixed a few thread safety bugs