Various fixes re. type variables

Fixes #75, #82 and #87 
Seems like the functional approach here makes the code worse, not better. (Not that that's new)