fixed up some bugs and added all tests.