added semaphores, made buffer global