ALMOST 25 years ago a Mexican president, Carlos Salinas, took a historic decision. He decreed that his country’s future lay in setting aside its fear and resentment of its mighty neighbour to the north and embracing economic integration with the United States through the North American Free-Trade Agreement (NAFTA). The agreement underpinned the modernisation of part of Mexico’s economy. So the imminent arrival in the White House of Donald Trump, a critic of NAFTA who threatens to build a migrant-blocking wall between the two countries, looks like a disaster for Mexico.

It would be easy to say that Mr Salinas made the wrong bet, as his many critics charged at the time. He didn’t. For Mexico, geography is destiny. Anyway, with $1.4bn in goods crossing the border each day, the country’s economy is now inextricably bound to that of the United States. So what is Mexico to do? Today’s president, Enrique Peña Nieto, who was excoriated when he invited Candidate Trump to visit, trusts that he can interest President Trump in a “modernisation” of NAFTA. He has some leverage: Mexican non-co-operation on trade, drugs and migrants could hurt the United States.

No democratic ally deserves the insults that Mr Trump directed at Mexico. But they have prompted introspection as well as anger. Mexico became “the easy piñata” of Mr Trump’s campaign because of its own failings, wrote Jesús Silva Herzog, a commentator, in Reforma, a newspaper. “The slamming of the door to the north leaves us, once again, face to face with ourselves.”

Mr Peña is right when he insists that not everything in Mexico is going badly. But many big things are. NAFTA has functioned as a legal exoskeleton, offering certainty to foreign investors. Domestic investors have no such luck. That is a big reason why economic growth has averaged less than 3% since 1990. Mexicans are fed up with out-of-control crime and what Mr Silva Herzog calls the “the permanent scandal of our public life”. Eight former state governors, all but one from Mr Peña’s Institutional Revolutionary Party (PRI), face corruption charges, having left their states with debts totalling $9bn. Only one is in jail.

The central problem that Mexico has evaded is that of governance. The country has only flourished when it has had a strong central government, albeit at the cost of liberty. That applied under Porfirio Díaz for more than 30 years until he was toppled by revolution in 1911. It applied again in the heyday of the PRI’s one-party system. With the defeat of the PRI in elections in 2000, Mexico gained political freedoms, but not the rule of law or accountable government, as Luis Rubio, a political scientist, explains in an essay for the Wilson Centre, a think-tank in Washington.

The power once monopolised by the PRI presidents is now shared with state governors and with the two main opposition parties. But there are still no checks and balances on its exercise, as the larceny of governors illustrates. And government is ineffective: Mr Peña has been unable to implement fully some of the reforms he enacted at the start of his term. If he thought the PRI’s old method of central command would work in a more sophisticated country, he has been disabused. He is widely reviled: his approval rating is only 25%.

If he wants to rescue his reputation he should use the remaining two years of his term to deal with the problem of governance, in two ways. First, he could appoint a genuinely independent attorney-general—an essential first step to establishing the rule of law. His government pushed through a law to grant autonomy to the office from 2018, but with the proviso that the incumbent would continue in the job for another nine years. His critics’ fears that the change will be merely cosmetic were raised when last month Mr Peña appointed Raúl Cervantes, a PRI senator and formerly the party’s lawyer, to the job. That is not good enough.

Second, he needs to tackle the declining legitimacy of politics and the presidency. Mr Peña was elected with just 38% of the vote. Because of growing political fragmentation, his successor may need only 25% or so. Whoever it is will find governing hard, unless Mexico introduces a run-off vote between the two front-runners in the presidential election, as most Latin American countries have.

Mr Peña has the negotiating skills to pull off these reforms. But does he have the will? Porfirio Díaz is once supposed to have exclaimed of his country: “Poor Mexico, so far from God and so close to the United States.” Today, its misfortune is that it is so close to Mr Trump and so far from good governance.