Some bugfixes and refactoring some of the tests