Refactor code into functions/Cleanup