Fix #82 #83 #88

056aca4 fixes #82
8702de5 and 2b5796c fix #83
ebd1a86 fixes #88

And 7168a3f is just a bonus, because I needed a better way to test this code while I was fixing the other bugs.
Sorry to lump these all in together, I guess I should have made a branch per bug.

Feel free to pick and choose.

Note that I left 2b5796c until last deliberately because it's the only commit that is actually a major change (although still compatible with everything else)