Better semaphore management + small fixes
Better semaphore management + small fixes