Refactored code to make it possible to accommodate multi-subpattern patterns