unit tests and some bug fixes