fix a bit of generated sql that was missing [  ]

plus some tidying up of comments, typos, etc