Add some tests, fix the code, and add some docs.