Fixed race condition bug on multithreading