improved the way to find the template