Added semaphore control to limit thread count