refactoring to remove duplicate code