added stopping criterion; needs substantial work