Refactored code into better functions.