Close, but no cigar.

With these changes, my (incomplete) tests pass, but no guarantee that it's 100% correct.