further improvement of the algorithm

stopping criteria implemented
and other things