Fix bugs for multi-thread