Refactoring: Performance and Concurrency problems.

Implemented Semaphores
Created static methods and field
TODO: Reduce the number of accesses to the database.