David F. Sandberg knows his comics, and with Shazam!, the director hoped to pay tribute to a classic DC rivalry between one world-famous Justice League member and the hero formerly known as Captain Marvel. But when things went wrong, it ended up making the movie’s surprise ending even better.

In an interview, Sandberg tells Inverse what he originally had planned for the end of Shazam!, what went wrong, and why he likes the final version of the movie even more.

Spoilers for Shazam! ahead.

At the end of Shazam!, Billy (Asher Angel and Zachary Levi) makes it up to Freddy (Jack Dylan Grazer) for embarrassing him in front of school. Earlier in the movie, after obtaining his powers from a dying wizard and becoming a superhero, Billy promises to show up to school as Shazam and sit next to Freddy at lunch. Instead, he busks on the Rocky Steps performing superhero tricks for cash.

Now, at the end of the movie, Billy comes all Shazam’d up, telling everyone at the school how cool of a guy Freddy is. That’s when Billy pulls out an extra trick by introducing a “friend” of his.

In walks Superman.

Seen only from the neck down (showing Superman’s beautiful DCEU costume), Freddy gasps when he sees the Man of Steel behind him, ready to sit down for lunch. Cut to credits.

“From day one, that was something we always wanted to do,” Sandberg says of the surprise ending. “Superman and Shazam have a bit of history.”

Sandberg is right: In the 1940s, during the Golden Age of comics and the industry’s first commercial boom, both Superman and Shazam were the mascots of competing publishers National Comics and Fawcett, respectively. While Superman came first, Shazam (then named Captain Marvel) was the medium’s superstar, eclipsing Superman in comic sales. It’s not hard to see why, either. What kid doesn’t dream of being a superhero at the crack of thunder?

In the 1996 Eisner-winner miniseries 'Kingdom Come', published by DC and created by Mark Waid and Alex Ross, Superman and Captain Marvel/Shazam cross paths in a homage to their historic rivalry. DC Comics

There was even more to the scene, Sandberg says. He almost had DC film star Henry Cavill reprise his role as Superman for the surprise ending. A handful of logistics prevented Sandberg from shooting the scene he envisioned in his head, but in a twist of fate, this was the best thing to happen to the scene.

“Originally, Superman actually sat down at the table, and they had a little bit of a conversation,” Sandberg says. “But when we were up in Toronto shooting, we couldn’t make it work.”

Production of Shazam! partially took place at a real middle school that was closed “for some sort of a holiday.” “We only had the time we had because we could only shoot when the kids weren’t there,” says the director. “And, of course, I wanted Henry Cavill to pop up. But he was unavailable, which was disappointing.”

Zachary Levi (left) and David F. Sandberg (right) on the set of 'Shazam!' Warner Bros. Pictures

Sandberg remembers feeling vexed. “It’s the end of the movie. What are we going to do?” Determined to stick as close as possible to the original idea, he enlisted Zachary Levi’s stunt double to wear Superman’s costume and opted to shoot the Last Son of Krypton only from the neck down. It didn’t calm Sandberg’s nerves, however.

“We had Zack’s stunt double do it, but I was worried. Is this gonna feel cheap? Is this not gonna work? To cut before you see his face, is this gonna feel cheap? Are people gonna feel cheated?”

Later on in post-production, Sandberg finally relaxed when he saw the scene cut together.

“It just made us laugh,” he says. “You see Freddy’s reaction and a hard cut to credits, and it’s just funny. It turned out better than what it was originally, where he sat down and had a little chat.”

Shazam! hits theaters on April 5.