stack.c refactored