Bugfixes and tests