Refactored locking.