More unit tests and fixes