Some unit tests and fixes