Tidied up code and comments