added some comments and tidy up