Lots of small (and maybe not so small) changes from tg.

Details in the commits.  Might fix a few bugs.