Refactoring and breaking code into functions