Bugfixes, tests passing