More unit tests, and some fixes to problems that the unit tests found