optimize xtemplate scope finding.Fixes #523