refactor common functionality