Fix unit tests. Fix bugs exposed by broken tests.