Fixed semaphore wait bug