When he was nominated in March as a candidate for Indonesia's presidential election, polls showed Joko "Jokowi" Widodo had a 30-point lead over the man who would become his main challenger, former army general Prabowo Subianto.
But after three months of shambolic and lackluster campaigning, the wildly popular governor of Jakarta was staring at defeat.
"If you continue like this, it's clear we will lose," prominent businessman Sofjan Wanandi told Widodo and senior members of his Indonesian Democratic-Party of Struggle (PDI-P) at a crisis meeting on June 16.
With election day less than three weeks away, Widodo had just been given a poll showing Prabowo trailed by only 1.8 points.
"'No more discussion. We have to start fighting'," Widodo told the stunned gathering at a Jakarta hotel according to Wanandi, a key financier of Widodo's campaign, recalling the drama in a recent interview with Reuters.
That meeting was the turning point. Galvanized by an army of young, social media-savvy volunteers, and by what one advisor called "fear of losing", Widodo's campaign kicked into gear. He eventually beat Prabowo by about six percentage points, with 53.15 percent of the 130 million votes cast on July 9.
Alleging "massive cheating", Prabowo still disputes the result and has so far refused to call Widodo to congratulate him on his win. On Aug. 21, Indonesia's constitutional court unanimously rejected his last-ditch bid to overturn it.
Decade of experience
Aides insist Widodo's amateurish campaign does not presage an equally tepid and directionless presidency. A close media advisor said the near-miss will make him a stronger leader.
"This will be a president who takes risks but who has learned a lot about the country through the campaign," he said. "He's like a sponge. He learns from people and from his experiences."
Lukewarm support from the PDI-P and its long-time leader Megawati Sukarnoputri - a former president who had coveted a return to power - blighted Widodo's early campaign. Many now wonder what influence Megawati will exert over the presidency.
"The campaign indicates there are certain key constituencies he will have to handle very deftly," said Jakarta-based political analyst Douglas Ramage.
Less than two years ago, Widodo was mayor of Solo, a city of 500,000 in Central Java. On Oct. 20, he will be sworn in as president of the world's fourth most populous country.
Despite this meteoric rise, some experts believe he has the skills to run Indonesia after winning plaudits for his business-like management of the teeming capital over the past 18 months and for transforming crime-ridden Solo into a center for art and culture.
"He might be inexperienced on the national stage, but what we often miss is that he's 53, with a decade of experience in executive positions," said Ramage.
Widodo assumes the presidency at a critical time, with Indonesia's growing confidence abroad matched by an unfulfilled domestic yearning for clean government and a more equitable society.
Indonesia under Widodo will be more "inward-looking, but that's not a bad thing," said Achmad Sukarsono, a political analyst at the Habibie Center, a Jakarta think tank. "We have a president who (understands) our interests, not just the interests of the few elites."
Widodo's campaign got off to a late start. The PDI-P didn't nominate him until mid-March, only then scotching speculation Megawati would run despite losing two previous presidential elections. By contrast, Prabowo's bid was years in the planning.
The rift between Widodo and the PDI-P widened after the April 9 parliamentary election. The party performed worse than expected. Its campaign, run by Puan Maharani, Megawati's daughter and heir apparent to the PDI-P chair, failed to capitalize on Widodo's popularity.
Instead, it promoted the notion that Megawati controlled the party - and, by implication, Widodo. This was a gift to Prabowo, who would urge people not to vote for a "puppet president".
Widodo later told Reuters he respected Megawati. Agitated, he added: "If there is someone who says that I'm a puppet, that is a big mistake."
Read MoreThis is Asia's investment moment
Even so, Megawati's debatable influence over Widodo was a drag on the campaign. "The party machinery was not working," said the close media advisor, who declined to be identified because of the sensitivity of the issue. "There was no coordination and everyone was just very relaxed."
On May 19, Golkar, Indonesia's second-largest party, unexpectedly threw its support behind Prabowo, and a Widodo presidency no longer felt like a sure thing.
Smears and volunteers
A smear campaign was also in full swing. Rumors spread on social media that Widodo, a Javanese Muslim, was an ethnic Chinese Christian - a tough sell in the country with the world's largest population of Muslims.
"The most frustrating part of the campaign was spending 70 percent of our time in Islamic boarding schools, countering the black campaign about Jokowi being ethnic Chinese and Christian," vice president-elect Jusuf Kalla told Reuters in a post-election interview.