Refactored code into functions.