Turkish President Recep Tayyip Erdogan’s victory in a constitutional referendum that would grant the presidency sweeping new powers wasn’t unexpected. What was unexpected: the thin margin by which he won. Unofficial results showed the “Yes” vote, which granted Erdogan broad powers, garnered 51.18 percent (24.3 million votes); the “No” vote received 48.82 percent (23.2 million).

“Erdogan has become the most powerful Turk, but while half of the country adores him the other half loathes him,” Soner Cagaptay, the director of the Turkish Research Program at the Washington Institute for Near East Policy, wrote on Twitter.

The referendum results mean that Turkey will adopt a presidential system from its present model of a parliamentary democracy in which the prime minister is head of government, and the president—a largely ceremonial position until Erdogan took office in 2014—head of state. Among other things, the vote would scrap the post of prime minister and transfer those powers to the president, who would also be empowered to appoint judges and issue decrees. Erdogan would have to win the 2019 elections to gain these powers, but with no viable rival appearing on the horizon, he appears set to be re-elected to another term.

The opposition is contesting the results of the vote, pointing to the last-minute change in rules by the election commission that declared as valid ballots without an official seal. Previously, ballots were stamped with the official seal and handed to voters, signifying they were, in fact, valid. This rule change, critics argue, made it easier to falsify the results of the referendum, which was criticized by European observers. Kemal Kilicdaroglu, the leader of the opposition People’s Republican Party (CHP), said as many as 1.5 million ballots without an official seal were counted (the “Yes” camp won by 1.2 million votes). The CHP and the People’s Democratic Party (HDP) both said they’d challenge the results.

Over the 15 years he has run Turkey (first as prime minister from 2003 and since 2014 as president), Erdogan has gained ever-greater powers even as the stature of the political opposition has diminished. Last November, authorities arrested two senior Kurdish members of the HDP, just months after Erdogan signed a bill lifting lawmakers’ immunity from prosecution. Just two years ago, the opposition had a chance to unite to form a coalition against Erdogan’s AKP. The ruling party had lost its parliamentary majority, giving the political opposition a realistic chance of coalescing against Erdogan. But the Nationalist Movement Party (MHP) refused to work with the pro-Kurdish HDP. Sensing opportunity, Erdogan called for elections in November 2015, which the AKP won in a landslide—ensuring the opposition was confined to political irrelevance.

But the results of Sunday’s referendum showed that despite increasing restrictions imposed by Erdogan on the media, civil society, and the bureaucracy—especially after last summer’s coup attempt against him—Erdogan has not succeeded in eliminating challenges to his rule. Turnout in Sunday’s referendum exceeded 80 percent. The country’s three largest cities—Istanbul, Ankara, and Izmir—all voted “No” to expanding the president’s powers. It’s the first loss Erdogan has suffered in Istanbul, the city of which he was mayor from 1994 to 1998.

“I think it's actually the ‘best’ outcome,” Cagaptay told The Washington Post. “If Erdogan had lost, this would have unleashed a period of instability as he would have gone for a rerun of the vote as many analysts predicted he would, and if he had won with a wide margin, he would ‘gone off the charts,’ becoming completely authoritarian. Now, his wings have been clipped and he has been humbled.”

Still, the results of the referendum could allow Erdogan to stay in power until 2029. Under the constitutional change that passed, he could serve two more five-year terms starting in 2019. If that happens, Erdogan would have governed Turkey for more than two decades, cementing his place as the most powerful Turk since Mustafa Kemal Ataturk, who founded modern Turkey nearly a century ago on the ruins of the Ottoman Empire.