Fixed multithread issues