Improves name-based patmat.

Some internal documentation and logic separation, and fixing the
specific-to-name-based-patmat bugs I know about.

Over to you, @adriaanm.