tidy up and add comments