Update docs and tests, and clean up implementation a bit.