Final version with debug code

Custom doesn't work perfectly, but it does better in _most_ cases.  And
the rest seems to work.