Refactored the template generator