Cleaned up some code, added some tests.