Refactor common functions