More tests and a couple of bug fixes