bugfixes and improved unit tests