Added algorithm for template sampling