Refactored the script into functions. Needs still work