Tidied up a lot of code and comments