Cleaned up.  Seems to pass all tests.