tidied up some code and comments