Refactor the stack frame handling for scripts