refactored added a method to replace duplicate code