refactoring duplicate code