Fixing nesting issues in templates