Added a bunch of new tests and fixed a lot of code.