Refactor to split out model generation code