Until the final hours, US diplomats who had spent eight days locked in a Lausanne hotel hammering out the outlines of what could be one of the most complex arms-control treaties ever feared their efforts would be in vain.
Three times US Secretary of State John Kerry's plane was put on stand-by to leave. Luggage was collected from his team of negotiators and staff as well as from the travelling press. Three times the flight was pushed back.
"It was quite a rollercoaster. We'd get close, we kept on changing the plane schedule. It would go, it wouldn't go. We had to reset the clock," said a senior State Department official.
In the last two days, they sensed the deal was close. "You know what the elements are, but knocking it out is another story altogether," the official said.
- Solving the jigsaw -
For the Americans, they had to reach a complicated formula of interlocking parts to ensure that it would take Iran a year to gather enough fissile material to build a bomb by reducing stocks and cutting back its facilities.
In return, Iran was seeking an end to a labyrinth of global sanctions, which Kerry was insistent would not all be lifted in one fell swoop while demanding a tough verification process to ensure Tehran was sticking by the deal.
It was the highly-organized Under Secretary Wendy Sherman, who has led the negotiations, who early on hit on the idea of a whiteboard as a way of illustrating what she has long referred to as a "Rubik's cube."
In Lausanne the whiteboard on wheels followed her from room to room as she and Kerry met with Iranian Foreign Minister Mohammad Javad Zarif and his team.
The idea was to try "to make sure that we and Iran were seeing the picture in the same way," another senior administration official said.
The whiteboard appealed to the Iranian team "because if they get paper, they've got to take it back to Tehran," the State Department official said.
Kerry had his own version committed to paper to consult on the go.
There was one heart-stopping moment when a negotiator used a permanent marker to write down classified calculations. They took some frantic rubbing out.
- Pulling an all-nighter -
Signup to our newsletter and follow us on Facebook and Twitter!
The breakthrough came during the night of Wednesday to Thursday, when Kerry and Zarif and a small team sat down and negotiated through the night.
In a room next door, the rest of the US staff, fuelled by constant calls to the hotel's room service, sat ready to be "plucked out" if needed.
Hundreds of staff back in the United States, especially at the California-based Lawrence Livermore national laboratory -- which specializes in non-proliferation and guarding nuclear arms stockpiles -- were on standby at the end of the phones.
"It was just a very integrated, very interactive back-and-forth kind of a process," the State Department official said.
In the end each side made compromises. Iran agreed its unfinished Arak heavy water reactor will not produce plutonium, slashed the number of centrifuges by two-thirds to 6,000 and will redesign its Fordo facility into a research center.
The Americans said 1,000 centrifuges could stay in Fordo, even though they won't be used.
"We gave some things that are hard for us. Having even one centrifuge in Fordo is hard," the State Department official said.
Inevitably there were last minute glitches as the talks resumed Thursday after Kerry snatched an hour's sleep, especially as all the other ministers from the so-called P5+1 also had to sign off on the deal.
But "a certain momentum takes over, everybody begins to realize it's going to happen, and that momentum helps to get the problem solved."
- Different narratives -
The deal done, both sides then turned to the task of selling the agreement to audiences back home.
It was agreed that while Zarif and the EU's foreign policy chief Federica Mogherini would make a common statement, Zarif and Kerry would hold separate press conferences.
Both Iran and the US wrote their own statements to put out to the media at a hastily convened press conference for about hundreds of reporters.
It was not lost on American diplomats, that the Iranian statement hit the press before the State Department's fact sheet. Nor has it gone unnoticed that the two statements have a different twist on what happened.
"We understood we would have different narratives, but we wouldn't contradict each other," the State Department official said.