Lots of bug fixes, now unit tests pass!