Reimplement thread pool

In order to fix found synchronization problems