Refactor code to be in functions