unit tests and various fixes