added semaphores

seems like a good idea. It can't crash anymore, but it may get sluggish.