improved unit tests and several bug fixes