refactoring for stacks, again, and lists too