added better stop condition