Add some unit tests and fix others