First pass.  Slow and ugly, but all tests are passing.