Added tests and fixed a few bugs that the tests threw up.
Really added the tests this time.