Template and code generation improvements