Bowing to pressure , President Donald Trump signed an executive order Wednesday ending the process of separating children from families after they are detained crossing the U.S. border illegally.

It was a dramatic turnaround for Trump who incorrectly insisted that his administration had no choice but to separate families apprehended at the border because of federal law and a court decision.

The news in recent days has been dominated by searing images of children held in cages at border facilities, as well as audio recordings of young children crying for their parents – images that have sparked fury, question of morality and concern from Republicans about a negative impact on their races in November’s midterm elections.

Until Wednesday, the president, Homeland Security Secretary Kirstjen Nielsen, and other officials had repeatedly argued the only way to end the practice was for Congress to pass new legislation, while Democrats said he could do it with his signature alone. That’s what he did on Wednesday.

“We’re going to have strong, very strong borders, but we’re going to keep the families together,” said Trump who said he didn’t like the “sight” or “feeling” of children separated from their parents.

Justice Department lawyers had been working to find a legal workaround for a previous class-action settlement that set policies for the treatment and release of unaccompanied children who are caught at the border.

Still, Trump’s order is likely to create a new set of problems involving length of detention of families, and may spark a fresh court fight.

Also playing a role in his turnaround: First lady Melania Trump. One White House official said Mrs. Trump had been making her opinion known to the president for some time that she felt he needed to do all he could to help families stay together, whether by working with Congress or acting on his own.

Trump had tweeted earlier Wednesday, “It’s the Democrats fault, they won’t give us the votes needed to pass good immigration legislation. They want open borders, which breeds horrible crime. Republicans want security. But I am working on something – it never ends!”

The administration recently put into place a “zero tolerance” policy in which all unlawful border crossings are referred for prosecution – a process that moves adults to the custody of the U.S. Marshals Service and sends many children to facilities run by the Department of Health and Human Services. Under the Obama administration, such families were usually referred for civil deportation proceedings, not requiring separation.

The policy had led to a spike in family separations in recent weeks, with more than 2,300 minors were separated from their families at the border from May 5 through June 9, according to the Department of Homeland Security.

Planning at the Justice Department had been underway over the past several days to provide the president with options on the growing crisis, said the official, who, like others, spoke on condition of anonymity to discuss the effort before its official announcement.

The person said Trump called the Justice Department Wednesday morning asking for the draft order. The official did not know what prompted Trump to change course.

The Flores settlement, named for a teenage girl who brought the case in the 1980s, requires the government to release children from custody and to their parents, adult relatives or other caretakers, in order of preference. If those options are exhausted, authorities must find the “least restrictive” setting for the child who arrived without parents.

In 2015, a federal judge in Los Angeles expanded the terms of the settlement, ruling that it applies to children who are caught with their parents as well as to those who come to the U.S. alone. Other recent rulings, upheld on appeal, affirm the children’s rights to a bond hearing and require better conditions at the Border Patrol’s short-term holding facilities.