The most important thing to understand about President Trump's decision Tuesday to wind down the DACA program, which has allowed 800,000 immigrants brought to this country illegally as children to stay here, is just how legally shaky DACA was.

Before issuing the executive order creating DACA – which stands for Deferred Action for Childhood Arrivals – President Obama had repeatedly said he did not have the authority to change immigration law on his own.

President Obama then changed his mind. The courts, including the Supreme Court in 2016, enjoined a similar law for adults. Legal scholars predicted DACA would be declared invalid within a year.

President Trump's decision to end DACA – but to allow a 6-month grace period so Congress can codify changes in the law that could let so-called Dreamers stay – is being attacked from left and right.

To liberals, the president’s move represents the creation of heartless uncertainty. For conservatives, immigration hawk Rep. Steve King, R-Iowa, says the president's action will touch off a "big, civil war" among Republicans.

In reality, the president is this time keeping his cool and navigating the turbulent political waters here prudently.

President Trump isn’t keeping the specifics of the DACA order, which grants a two-year renewable work permit to recipients while blocking any deportation actions against them unless a criminal record is involved. But President Trump says he will work with Congress to reach a viable solution.

A statement by the president Tuesday echoes DACA supporters in one respect: "I do not favor punishing children, most of whom are now adults, for the actions of their parents," President Trump said.

Candidate Trump’s heated campaign rhetoric last year led many people to believe he really did want to deport millions of undocumented immigrants. But his attention-grabbing soundbites were belied by his previous positions on the subject.

Back in 2012, when he was mulling over a run for president, Trump told an interviewer he was “probably down the middle” on immigration issues. After his election in 2016, he said he would focus on deporting "bad hombres" with criminal records and leave the Dreamers alone while the issue was sorted out.

The genius of Trump on the campaign trail was that he sent two different messages on immigration. On the one hand, he called for a border wall and bizarrely claimed he would get Mexico to pay for it. On the other hand, he said he would build a “big beautiful door” in the wall to let in legal immigrants. He reiterated that stand after the election, when he told a rally that he expected the wall would let in both legal immigrants and guest workers.

With his acknowledgement Tuesday that he expected to sign a compromise on the Dreamer issue, President Trump is actually proving himself a good player in the game of the political two-step. Activists on both sides of the political spectrum are upset, but the vast majority of voters in the middle will probably see it as a reasonable compromise.

Of course, members of Congress are already posturing. Rep. Joe Crowley, D-N.Y., is accusing Trump of "hostage taking" should the president try to tie Dreamer status to, say, funding for his border wall. But in the end, the political onus will be on Democrats if they block Dreamer legislation because they refuse to link it to any other issue.

If President Trump plays his cards right, his move Tuesday is smart politics. With new guidelines in place that make it easier to deport unwanted or criminally minded immigrants, the president can feel free to be flexible and even generous with other immigrants.

The end result will satisfy neither President Trump’s most fanatical supporters nor his critics. But it is certainly likely to work better than the chaotic status quo on immigration.