tidy up and add some comments