Refactor stack functions