cleanup stack implementation