Refactored the template generation.