preliminary algorithm 1 implementation