Fixed multithreading bug