Fixed a few bugs to pass tests, and fixed a few bugs in tests.