Factorize generation code