Split internal functions in subpatterns.py out into a new internal module