WASHINGTON — President Donald Trump said he would be signing an executive order later Wednesday that would end the process of separating children from families after they are detained crossing the U.S. border illegally.

“We want to keep families together. It’s very important,” Trump told reporters during a White House meeting with members of Congress. “I’ll be signing something in a little while that’s going to do that.”

The effort would mark a dramatic turnaround for an administration that has been insisting, wrongly, that it has no choice but to separate families apprehended at the border because of the law and a court decision.

Homeland Security Secretary Kirstjen Nielsen, the president and other officials have repeatedly said the only way to end the practice is for Congress to pass new legislation, though both Democrats and some Republicans have said the president could reverse it by himself.

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.

Trump pointed to those images in his meeting, saying they’d “affect everybody” but that he was torn. “We want the heart,” he said, “but we also want strong borders and we want no crime.”

Also playing a role: 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 it was by working with Congress or acting on his own.

On Capitol Hill, some who have criticized the policy sounded cautiously optimistic.

If the president goes through with the signing, “It would be a complete 180. Clearly the president is hearing the uproar from our communities,” said Rep. Pete Aguilar, D-Calif.

Nielsen was to travel to the Hill Wednesday afternoon to brief lawmakers. And members on the fence over pending immigration legislation were also heading to the White House to meet with Trump.

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.

READ NEXT: ‘Free our children now’ — On the U.S.-Mexico border, activists protest separation of immigrant families

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.

The action, according to people familiar with it, wouldn’t end the zero tolerance policy, but would aim to keep families together while they are in custody and ask the Department of Defense to help house the detained families.

Justice Department lawyers have been working to find a legal workaround for a class-action lawsuit settlement that set policies for the treatment and release of unaccompanied children who are caught at the border, or crafting an order that would defy the settlement and force it back into court to argue for changes.

Two people close to Homeland Security Secretary Nielsen said early Wednesday that she was the driving force behind the plan to keep families together after they are detained crossing the border illegally.

One of the people said Nielsen, who had become the face of the administration’s policy, had little faith that Congress would act to fix the separation issue and felt compelled to act. Nielsen was heckled at a restaurant Tuesday evening and has faced protesters at her home.

But others pushed back on the idea that Homeland Security had led the rollback. One official said it was the Justice Department that generated the legal strategy that is codified in the executive order, and disputed the notion that Homeland Security was involved in drawing up the document.

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.

In 2016, the 9th U.S. Circuit Court of Appeals ruled that child migrants who came to the border with parents and were held in custody must be released. The decision did not state parents must be released. Neither, though, did it require parents to be kept in detention, apart from their children.

Associated Press writers Zeke Miller, Ken Thomas and Alan Fram contributed to this report.