Modified a bit to correct the logic written for pop