All tests passing after a substantial rewrite.