refactored code into functions