Refactoring: extract function with equality checking