make the termination condition more robust