On the day BuzzFeed News visited the Star Trek Beyond set in July last year, director Justin Lin was shooting a scene in which Captain Kirk (Chris Pine) receives some urgent news from his crewmates Uhura (Zoe Saldana) and Sulu (John Cho).

"Captain, this thing that he has…" Uhura said, alluding to Beyond's villain Krall (Idris Elba) and his vaguely powerful weapon of mass destruction.

"Yorktown," interjected Sulu, referring to the Federation outpost where Sulu's husband and daughter were stationed. "He's going to destroy Yorktown." Jaylah (Sofia Boutella), an alien with striking white-and-black skin, turned to Kirk and fixed him with a fateful stare. "You take my house," she said, referring to the grounded ship in which they were all standing. "You make it fly."

The scene unfolded with the same kind of cryptic urgency as just about any other summer action movie. But then Kirk looked over at Scotty (Simon Pegg), his trusty engineer, and the dialogue suddenly took on a decidedly nerdier tone.

"Scotty, can you get this thing started?" said Kirk.

"Started, yes," said Scotty. "Flying, that's another thing, sir. These old vessels, they were built in space. They were never supposed to take off from atmosphere."

In an interview almost a year later, Pegg didn’t even try hide his pride at composing that geeky line.

"Did you notice that Scotty says that starships are built in space?" the Beyond co-writer said in a Los Angeles hotel suite, grinning from ear to ear.

It was a small but telling detail in Pegg and Doug Jung’s script, which was written at a breakneck pace and filled with knowing nods to 50 years of Star Trek geekery. "If you come to it with some history, then that will be an enriched meaning there for you," Pegg said. "And there's so much stuff in it for people who were there in September 1966" — the month Star Trek first premiered on TV.

BuzzFeed News spoke with Pegg and Jung about how they somehow managed to finish the job — warning, the SPOILERS start here.