Refactor multi-thread code.