Some bugfixes and unit tests.