Fixed potential multithread bug