Fixed multithread bugs