Tidied some things up, refactored some code.