refactored the template logic