further improve code related to PatternMatcher

not ready for review