fix some bugs and add tests. yay tests.