Refactoring of the locking system for multithreading