Tony Stark was marked for death more than four years ago. From there, it was just a matter of how to do it. The why had already been decided.

Almost everything else about the Avengers films Infinity War and Endgame changed over and over again. Superheroes beating up themselves in a mirror world. Smart Hulk appearing one movie early. Before this played out onscreen, it appeared first on a magnetic whiteboard covered in scrap paper, colored notecards, and one-of-a-kind superhero trading cards that were marked with ascending dollar sign levels—to keep salary costs in mind.

That conference room at Marvel Studios was where screenwriters Christopher Markus and Stephen McFeely were mapping out the two-part Avengers finale that brought closure to the multibillion-dollar Marvel Cinematic Universe and became two of the highest-grossing movies of all time.

Their whiteboard was one of the most closely guarded secrets in the world. Even the actors didn’t know all the details during shooting.

What fans still don’t know, even today, are the alternate versions—the diversions and dead ends, the reshoots and the reasoning that went into concluding the story lines (and sometimes the lives) of some of the most beloved characters on the planet.

While criticism from Martin Scorsese and other filmmakers has sparked debate about whether superhero films qualify as cinema, the screenwriters described a creative process aimed at uncovering surprises, heart, irony, and emotion that they hoped would resonate for moviegoers the way pop classics have for past generations.

At a Q&A at the Writers Guild of America West, Markus and McFeely told Vanity Fair how it all played out.

The Story Begins...

Anthony Breznican: You both wrote not only on Infinity War and Endgame, but all the Captain America movies—The First Avenger, The Winter Soldier, and Civil War—as well as Thor: The Dark World and Agent Carter. When you’re writing these other movies, are you also creating long-term plans for where you’re going years ahead of time? When would you say you truly began to consider what the final Avengers stories would be?

Christopher Markus: At the very end of [2011’s] The First Avenger, when Steve misses the dance with Peggy, you start going, I wonder if there’s going to be a way to get those two together...even though 70 years has passed.

Stephen McFeely: But we have not been writing the movie for 10 years. We only wrote for four years.

Markus: I mean, we didn’t start formulating Endgame until we got the job, of course. Otherwise we’d be insane.

McFeely: No, the guild doesn’t let you write for free. When Civil War came out, we’d already finished the first draft of both Infinity War and Endgame. We got the job as we were prepping Civil War. So that meant that Kevin Feige, the head of Marvel, had hired me, Chris, and [directors] Joe and Anthony Russo simply based on The Winter Soldier, to do both the Avengers movies.

Markus: ...And lots of sitting in a room planning Civil War. We nearly had a nervous breakdown.

McFeely: I bring it up because it’s such a leap faith, right? We’d only had one movie as a foursome under our belt. We got the job, thought about it all throughout the shooting of Civil War. And then the last four months of 2015, we cracked both those [Avengers] movies. So Tony’s death and Cap’s dance were on three-by-five cards in September of 2015.