Added unit tests, stomped on some bugs.