update algorithm with stack