refactored code to emliminate duplication