Tidy up and add some comments