More unit tests (and fixes for some small bugs found writing them)