A few small bug fixes, and some extra related unit tests