Refactoring lock detection code