tweaked alg 2