Refactoring FunctionWithEnv to also take the stack as an argument.