Semaphores working, but there are optimization bugs that need fixing