Some bugfixes, tests now passing.