further improve code related to PatternMatcher

Nothing really interesting, mostly refactoring.