More thorough unit tests and fixes