Fixed scoping issue in templates