Corrected the algorithm of stack