Various fixes re. type variables

Fixes #75, #82 and #87 
+1
This was done mostly by trial and error, and debug+inspection in the context of the added unit tests.