Refactor common logic