pop algorithm