Fix #48

And a bunch of related tests