Bugfixes + tests