Template logic fixes