second algo
close to first assignment