optimize loop overflow