Major refactor to clean up all the junky coding I added.

Hopefully it will be easier to add a bit more functionality now.