Solution with semaphores, not quite working