The last lines were  especially tricky. I did my best to make sensible coding choices.  -- Charlie