Refactor lock logic into a common class.