refactoring bgtools.utils.dpatterns