Fix multithreading problem