move pattern creation out of the loop