semaphore refactoring