Call it the great Brexit reshuffle.

Poland has greeted the prospect of the United Kingdom leaving the European Union with fear that it will be relegated to the sidelines. London, after all, has been Warsaw’s strongest ally – and an advocate for the interests of non-euro members big and small.

But now, on the eve of Brexit, Poland’s European star is suddenly rising – thanks to an unexpected partner: Germany.

Why? In Poland, a common saying goes: When you don’t know what it’s about, it’s about money. In this case, it’s about Poland’s booming economy.

In the first half of this year, Poland became a bigger trading partner to Germany than the U.K.: Trade between Poland and Germany rose by 6 percent in the first half of 2019, even as German trade with the U.K. dropped by 3.5 percent.

For the past three decades, Poland has consistently moved up the global prosperity ladder.

This reordering of the ranking is bringing about a deep political shift in Berlin, with big consequences for Poland – and for the EU.

Poland’s economic growth has been relentless. For the past three decades, the country has consistently moved up the global prosperity ladder, as it slowly closes the gap with the EU average.

Germany — whose own economy is wobbling ahead of a potential no-deal Brexit – has taken note.

In a visit to Budapest in August, Chancellor Angela Merkel emphasized Germany’s cooperation with the Visegrad Group — the Czech Republic, Hungary, Poland and Slovakia — adding that she hopes Brussels would continue to support them with EU budget subsidies.

In doing so, the German leader effectively downplayed concerns over democratic backsliding in the region – pushing back against calls by some EU politicians to cut off funds from countries, like Poland that don’t respect the rule of law.

Indeed, in Berlin the need to maintain good economic ties appears to be trumping concerns about breaches of the rule of law. And nowhere more so than when it comes to Poland, where there’s a growing realization that the bigger the country, the greater the potential for backfire if Berlin tries to punish it.

It’s a remarkable shift for a country that in less than half a decade has gone from model European to problem child.

Since coming to power in 2015, the ruling Law and Justice (PiS) party has acted antagonistically and schizophrenically on the EU stage. Warsaw has tried to block the reelection of Donald Tusk, a former Polish prime minister, as president of the European Council. It stuck close the U.K. even after Brexit and has done much to test relations with neighbouring Germany and France, another key strategic partner.

But recently, Warsaw seems to have changed tack, recognizing that, with the U.K. on its way out, it can’t go on antagonizing Brussels – and more importantly, Berlin – or it will risk ending up completely isolated.

Things took a turn toward reconciliation in March this year, when Poland’s chief diplomat, Jacek Czaputowicz, shifted the government’s foreign affairs priorities to mend ties with its European partners.

The government’s decision to nominate Szymon Sekowski vel Sęk – a vocal advocate for a stronger Berlin-Warsaw alliance who has high political ambitions – as its deputy foreign minister is another sign Warsaw is serious about taking steps in Germany’s direction.

PiS ministers also presented a host of new business initiatives in Berlin earlier this summer, which German Economy Minister Peter Altmaier praised as being in line with Berlin’s Industry 4.0 plans. After the European election in May, the party threw its support behind Commission President-elect Ursula von der Leyen.

Poland’s governing party may still be a pariah to the main political European families, and its efforts to undermine the judiciary are sure to continue ruffling feathers in Brussels and beyond.

But PiS has a trump card: Germany is increasingly aware of the net benefits to its economy of keeping up investments in Poland, including the EU’s cohesion funds.

A closer alliance with Poland could push Germany toward a more assertive foreign policy.

The new Polish-German rapprochement has implications that go far beyond the two countries’ borders. In addition to shifting the balance of power within the EU, it has the potential to change Berlin’s stance globally.

A closer alliance with Poland could push Germany toward a more assertive foreign policy. When it comes to defence, the Poles have often argued that Berlin is punching below its weight – in marked contrast with a bipartisan commitment in Warsaw to defence spending and the transatlantic relationship.

With the Brits on the way out, Poland has realized that if it wants a stronger voice on the EU stage, it’s in its best interest to shore up its relationship with Berlin.

And with Berlin focused firmly on the economy – its most pressing concern at the moment – Warsaw has a real opening to do so.

This article was originally published on Politico Europe.

CORRECTION: This article was updated to clarify the relative size of Poland’s trading relationship with Germany.