refactoring and removing duplication