Fixed semaphore usage issues