Refactoring a lot of code to functions.