refactor out some shared functions from patterns