algorithm 1 not fast enough :(