added a bunch of unit tests, fixed a bunch of bugs