Fixed some bugs and cleaned up tests.