Add some comments and tidy up the row generation code