refactors TemplateGenerator, some new implementation