Austria and Turkey have two things in common. Both are undergoing major political changes. And both are needed by Angela Merkel to stem the flow of migrants and refugees wanting to reach Germany. The political shifts in both countries do not augur well for the German chancellor.

In Austria, society has become deeply polarized, as the presidential election that took place on April 24 and May 22 confirmed. Immediately after the second-round runoff, Norbert Hofer, who represents the right-wing Freedom Party of Austria, was running neck and neck with Alexander Van der Bellen, a former Green party leader. The result was decided by postal votes: Van der Bellen squeaked through.

The outcome matters for Merkel and for Europe. Although Hofer lost, his wide appeal—and his ability to push the established conservative Austrian People’s Party and center-left Social Democrats out of the presidential race—has shaken Austria’s comfortable postwar consensus. The traditional political elites were so taken aback by Hofer’s dramatic rise that the Social Democrats’ Werner Faymann was forced to resign as Austria’s chancellor on May 9.

Faymann had forged a close relationship with Merkel and had cooperated with her at the height of the refugee crisis. But the Freedom Party was able to tap into the growing fears and anxieties of a country that was taking in many refugees from the Middle East. As a result, Faymann, with the agreement of governments in the Western Balkans, closed off the route through that region used by refugees. Yet that wasn’t enough to save his political career, as the presidential campaign showed.

The new chancellor, Christian Kern, has promised a fresh beginning for Austrian politics, which over the years had become sewn up between the conservatives and the Social Democrats. But the increasing popularity of the Freedom Party and the ongoing refugee crisis are changing the face of Austrian politics and Europe’s political landscape.

Other nationalist and Euroskeptic parties, particularly France’s National Front, are benefiting from a (tolerated) public discourse that is unashamedly anti-immigrant and anti-Muslim.

Germany is not immune to these trends. Merkel’s conservative Christian Democratic Union is alarmed by the increasing support for the nationalist and anti-immigrant Alternative for Germany. The party has made big gains in recent regional elections. It is siphoning support away from conservatives and particularly from the Social Democrats, Merkel’s coalition partners. The Social Democrats have hit a new low in opinion polls, falling to just 20 percent.

No wonder Merkel no longer says “Wir schaffen das” (“We can manage”), the rallying cry she used when she threw open Germany’s borders to more than 1 million refugees. Instead, she has outsourced the refugee problem to Turkey. It was Merkel who pushed through a deal with Ankara under which the EU would provide €6 billion ($6.7 billion) in return for Turkey taking back refugees who had entered the EU illegally through Greece. More fundamentally, the deal was coupled with an offer of visa-free travel for Turks entering the EU provided that Ankara changed its antiterrorism laws.

Merkel negotiated the deal with Ahmet Davutoğlu, whom she considered her trusted interlocutor. He, however, was forced to resign as Turkish prime minister after he questioned the political direction being pursued by Turkish President Tayyip Recep Erdoğan.

Any pretense or illusions about Erdoğan’s ambitions to give the presidency strong powers and move the country toward authoritarian rule needed no further proof when on May 20 the Turkish parliament voted to lift the immunity from prosecution of a quarter of its deputies. In practice, the opposition, especially Kurdish parliamentarians, have been silenced.

Before arriving in Turkey for a visit on May 23, Merkel criticized the lifting of the immunity. That criticism is likely to carry little weight. Merkel needs Erdoğan to implement the EU deal on refugees despite his continuing clampdown on the media and the Kurds. The deal could well unravel. The European Parliament and several political parties across Europe are loath to pander to Erdoğan and equally loath to grant visa-free travel until he meets his side of the bargain.

None of the above is good news for Merkel. The refugee crisis has upset the predictability of politics across Europe in a way that Erdoğan and the Freedom Party of Austria have been able to exploit. Merkel is now in the highly uncomfortable position of having to deal with interlocutors with questionable values and of having to consider what happens if the refugee deal doesn’t stick.