On the afternoon seven years ago when Rio de Janeiro was named the first South American city to host the Olympics, thousands of Cariocas—the Portuguese name for Rio locals—stormed the city’s storied Copacabana beach in an outpouring of national pride. At the International Olympic Committee’s meeting in Copenhagen, Brazil’s delegation hugged and sang the samba anthem that trumpets Rio as the Marvelous City. “The world has recognized that the time has come for Brazil,” proclaimed then-President Luiz Inácio Lula da Silva. It seemed as if the eternal country of the future had finally arrived in the present.

The tune is far different on the eve of the Aug. 5 Opening Ceremony. With its breathtaking scenery and intense lust for life, Rio may still be “full of a thousand charms,” as the lyrics of Marvelous City have it. But rather than welcoming the world to its global coronation, Brazil is scrambling to prevent the Games from a becoming a close-up view of the nation’s implosion. In the runup to the Olympics, Brazil has become mired in a political crisis that could lead to the impeachment of its president. An economic crisis has sent unemployment soaring and left police unpaid, contributing to a rise in robberies and violent crime. On top of the old demons of crumbling infrastructure and rampant corruption, the newer, more personal threat of the Zika virus has scared off international visitors and star athletes like golfers Jason Day and Jordan Spieth. Domestic ticket sales have been lackluster.

With each setback, there is more talk that the games are cursed. In April, part of an elevated cycling lane meant as an Olympic legacy project collapsed into the sea, killing two. In the Amazon rainforest, a jaguar that had taken part in an Olympic torch ceremony was shot dead after escaping its handlers shortly afterward. The state government is virtually bankrupt and hospitals and schools are in disarray. Protesting police summed up the mood on June 27 with a banner at the city’s international airport. “Welcome to Hell,” it read, with another placard noting the number of officers killed in the state this year, 49 according to officials. “Rio is simply not in the mood for the Olympics,” admits Marcelo Freixo, a prominent local politician.

But Rio de Janeiro remains a city that knows how to throw a party. The 2014 World Cup and a 2013 visit by Pope Francis passed without major problems, likewise the annual Carnival and New Year celebrations. And despite the July 21 arrest of 10 members of a Brazilian Islamist group who were allegedly plotting a terror attack, officials remain confident that the Games will be safe.  All of which begs the question: can Rio pull it together in time to make the 2016 Summer Olympics pay off at least some of the promise that loomed so large seven years ago?

The answer matters to more than just the expected 10,500 athletes and 500,000 foreign visitors. These Games were first about cementing national pride–and revitalizating Rio itself. Founded 451 years ago, the city of 6.3 million was a glamorous destination for most of the 20th century. But after losing its status as Brazil’s capital in 1960, Rio slid into decline. The federal government moved to the planned city of Brasilia, while the stock exchange and major banks decamped to São Paulo. Rio’s beautiful façade remained but behind it was rot: corruption, organized crime and severe poverty characterized a city where hillside favela slums controlled by armed gangsters existed within earshot of opulent beachside apartments.

By the time Rio was awarded the Olympics in 2009, beating out Chicago, Madrid and Tokyo, ambitious renewal already was underway. Two years earlier, billions of dollars worth of oil had been discovered off the coast, promising economic transformation. The soccer-mad nation was set to host the World Cup and Silva, then at the peak of his power—in 2009 President Barack Obama called him “the most popular politician on Earth”—was spreading the proceeds of the booming economy to lift tens of millions of Brazilians out of poverty. “We were experiencing the height of Brazil-mania,” says Marcos Troyjo, co-director of the BRICLab at Columbia University, which studies emerging nations led by Brazil, Russia, India and China. “There was this belief that everything was going right for Brazil.”

As with Tokyo in 1964 and Beijing in 2008, the Games were seen inside and outside the country as a chance to show how far Brazil had come. But Brazilians also hoped that the Olympics would provide the money and will to finally overcome decades of lagging infrastructure and security failures. Officials pledged to retake territory from the gangs that ran much of Rio and install policing units in many of the city’s favelas; to redevelop the rundown port; and to build a new subway line linking Rio’s main beach zone to the Olympic Park. The bid organizers even promised the IOC that the state government would clean up the notoriously polluted Guanabara Bay—where the sailing events will be held—which had long been used as a sewer by much of the city. Rio would “set a new standard of water quality preservation for the next generations,” they said in their submission.

But Brazil’s luck turned sour before the Olympic torch arrived. The global market for export commodities that powered the country’s economy— such as iron ore and sugar— crashed from 2014, leading to a recession that could end up being the worst since record-keeping began in 1901. Last year Brazil’s GDP shrank 3.8%, and is forecast to fall even further this year. The country’s political class has imploded even more dramatically. What began as a modest police investigation into money laundering has spiraled into the biggest corruption scandal in Brazilian history, with dozens of politicians and officials from the state oil giant Petrobras accused of taking billions in bribes. The epic scandal produced widespread public anger and single-digit approval ratings for President Dilma Rousseff, Silva’s handpicked successor.

What happened next is the subject of furious debate in Brazil. Rousseff, who has not been personally implicated the scandal but was chair of Petrobras at the time, was suspended by the senate in May on unrelated charges of manipulating the budget ahead of her 2014 reelection. For that, she faces an ongoing impeachment trial, which may yet conclude before the Games finish on Aug. 21. She says she has committed no crime and was the victim of a “coup” at the hands of congress and of her Vice-President Michel Temer, who is now serving as interim president. “It’s as if the constitution has been torn,” Rousseff tells TIME. “It hurts the rule of law and injures democracy. So I am not only fighting for the preservation of my mandate, I am fighting because democracy in Brazil was very hard won.”

Conditions are particularly bad in Rio itself. Plunging oil prices led the state government in June to declare a “financial emergency” so it could receive $890 million in federal funds to avoid [I’m not sure the collapse has a name per se] a “total collapse in public security, health, education, transport and environmental management.” The crisis has been so severe that police have been going without not only paychecks, but fuel for patrol cars and even office supplies. Amid the discord, Rio state in May recorded more than 300 street robberies a day, up 85% from 2012. The murder rate, however, is still 35 percent down on its peak a decade ago.

The rising crime rate raises questions about the safety of these Games far beyond the threat of terrorism, which is ever-present at large-scale international events. Officials say there will be 68,000 security personnel on Rio’s streets during the Olympics, with 21,000 troops supplementing 47,000 police. That may be enough to secure the hundreds of thousands of foreign visitors, but perhaps at a cost to Rio’s own citizenry. “The security of visitors to the Games will be assured,” says Robert Muggah, a security analyst at the Igarapé Institute think-tank. “But resources are so tight the risk is that crime in other areas of the city significantly increases as a result.”

Plenty of other problems remain. Only 76% of Olympic tickets have been sold so far; 1.5 million remain. Unsold tickets will deprive the organizing committee, already facing an intense budget squeeze, of further revenue. Guanabara Bay has not been cleaned up, and though officials insist it will be safe for the world’s best sailors, tests by the Associated Press have found levels of human adenoviruses equivalent to raw sewage in the water. Trash collection vessels regularly gather debris —including dead animals—that might impede boats.

Still, Rio should squeak in under the wire. The pace of construction, which the IOC two years ago called “worst ever,” stepped up enough that all venues should be ready for the Opening Ceremonies. (Although an allegation that the former state governor of Rio asked for a bribe of 5% on the $370 million redevelopment of the Maracanã stadium is a reminder that corruption shadows these Games.) The $5.7 billion subway extension—a crucial transport link in a city choked by traffic on normal days—is set to open just five days before the Games begin. “It would be seismic incompetence if they could not pull off the actual Olympics,” says Jules Boykoff, the author of Power Games: A Political History of the Olympics. “So long as the metro opens on time, they should be fine.”

But even if the Games themselves are a relative success, they are not likely to jumpstart Rio. Pensions and salaries have gone unpaid, schools have been paralyzed by strikes and, at one point, some hospitals shut to all but emergency patients. The battle to reclaim territory from gangs is suffering setback after setback, with shootouts now a regular occurrence in many supposedly pacified favelas. Plans to implement a $2.6 billion blueprint to make the favelas a formal part of the city, including improving sanitation, roads and lighting, has for the most part not materialized.

It turns out, 2016 is not the best moment to be in the eyes of the world. But here it is, as scheduled. And, in the end, the Carioca spirit of optimism may yet salvage what the IOC was hoping to find in Brazil. The thousands now set to descend on the Marvelous City will likely come away impressed by the verve of a metropolis that loves a big occasion. What will be left for those who remain is another matter.