Tidy up and add comments