Refactored code generation to jinja2 templates