bugfixes and unit tests