Fix bugs in multi-thread processing