stack management refactor