Lots of bugfixes to make the tests run