Refactored the code into something with a little more structure
to prevent me from going insane.