Template detection improved