a more efficient alg