refactor to remove code duplication