Added semaphores and locks. Restructured thread sleeping.