Refactor threadpool support. Bug fix.