Bugfixes, more unit tests