added stopping criterion