moved template logic to before function