Fix multithreading issues