backport more of onbehalf.tpl changes