Misc template enhancements and optimization