Refactoring of thread pool code