Bugfixes and unit test fixes