Refactoring to remove duplicate code