Added a bunch of tests and fixed a few bugs.