Generalize common pattern into function.