refactor existing code, remove duplication