move templating code to templates