Separate mass of code into more modular format.