refactor patternMatch