Many legal experts have spotted a clear tension between this week’s close 5-4 Supreme Court decision upholding President Trump’s travel ban, and the court’s very recent 7-2 ruling in Masterpiece Cakeshop v. Colorado Civil Rights Commission.

The cases raised similar issues, yet the justices went in opposite directions. Both decisions involved seemingly neutral government policies that were challenged in part because of evidence showing the true purpose behind them was discrimination on the basis of religion.

In Masterpiece Cakeshop, the court ruled in favor of Jack Phillips, a baker who refused to prepare a cake for a same-sex wedding, because he opposes such marriages on religious grounds. He prevailed because the justices found that some members of the Colorado Civil Rights Commission, which had adjudicated his claim, expressed bias against Phillips’ religious beliefs.

Donald Trump, meanwhile, famously called for a “Muslim ban” on the campaign trail — then instituted a travel ban that targeted Muslim-majority nations. Yet the court upheld the travel ban, and the majority concluded that Trump’s statements should not determine the outcome of the case. All five justices who voted to uphold the travel ban were also in the majority in Masterpiece Cakeshop.

This contradiction can’t be justified. The main rationalization for it — that immigration policy is largely exempt from normal judicial scrutiny — is badly flawed. Immigration and national security policy should not be exempt from the usual constraints of the Bill of Rights. Other possible justifications for the discrepancy also fall flat.

There was much stronger evidence of bias in the travel ban case than in Masterpiece

Jack Phillips prevailed, according to the majority in Masterpiece Cakeshop, because the Colorado Civil Rights Commission showed “elements of a clear and impermissible hostility toward the sincere religious beliefs that motivated his objection.” It was the words of the commission members that did them in. One member said it was “despicable” for the baker to use “freedom of religion ... to justify discrimination.” Another compared the baker’s views to the use of religion to justify slavery and the Holocaust.

The comparison between refusal to bake a cake and the Holocaust was indeed absurd, and it is reasonable to see that statement as evidence of not just foolishness but bias.

But as Justice Sonia Sotomayor points out in her dissent in the travel ban case, the evidence of discriminatory motivation on Trump’s part is far stronger. The travel ban proclamation followed numerous statements by President Trump explicitly advocating a “Muslim ban” (dating back to a December 2015 policy proposal calling for a “total and complete shutdown of Muslims entering the United States”).

When Trump eventually switched to a “territorial” policy focused on Muslim-majority nations, he repeated equated that new policy with the earlier “Muslim ban” proposal. He even described the latter as an “expansion” of the former.

What’s more, the discriminatory motivations expressed in Trump’s statements played a far bigger role in the travel ban policy than the two commisioners’ biases did in the original adjudication of Masterpiece Cakeshop. As Justice Ruth Bader Ginsburg pointed out in her Masterpiece Cakeshop dissent, the two officials who expressed prejudice against Phillips did not by themselves determine the outcome.

The case was first considered by the Colorado Civil Rights Division (which found “probable cause” for a violation of state antidiscrimination law), then assessed by the Civil Rights Commission, then heard by an administrative law judge, and finally reviewed by the Colorado Court of Appeals.

Only two of seven members of the Civil Rights Commission expressed hostility towards the baker’s beliefs religious beliefs; there was no indication of hostility on the part of the other three bodies that considered the case.

By contrast, where the travel ban is concerned, there was only one ultimate decision-maker: Trump. His motives are the ones that matter.

Chief Justice Roberts’s opinion for the court in Trump v. Hawaii endorses the administration’s claim that the travel ban was not based on Trump’s incendiary remarks on the campaign trail but rather rooted in a “worldwide review process undertaken by multiple Cabinet officials.” But that supposedly thorough review produced a cursory report only 17 pages long. And the resulting proclamation does not even consistently apply the criteria supposedly used to decide which countries effectively share information used to preventing terrorism.

Longstanding Supreme Court precedent holds that a seemingly neutral government policy can be invalidated if there is evidence that its true purpose was discrimination on the basis of prohibited classifications, such as race, ethnicity, or religion. But the government can potentially still prevail if it proves that the policy would likely have been enacted for legitimate reasons even in the absence of illicit motivations. By that standard, too, the case for upholding the Colorado ruling in the cake-shop case is stronger than that for upholding the travel ban.

The official security justification for the travel ban policy is laughably weak. In the 40-year period for which there is data, no one has ever been killed in a terrorist attack in the US by an entrant from any of the nations covered by travel ban. Combined with Trump’s statements and the highly dubious “review process, this strongly suggests that the official rationale for the policy was a pretext.

By contrast, there was a plausible rationale for Colorado’s decision: preventing discrimination against same-sex couples, whether or not such discrimination had religious motivations.

All efforts to distinguish the two cases fall short

In upholding the travel ban, the Supreme Court majority ruled that the policy was subject only to the most minimal “rational basis” review because it involves immigration policy, and immigration policy is relevant to national security. That policy realm, Chief Justice Roberts wrote, involves issues where the executive has specialized expertise that judges lack.

But there is no good reason to exempt immigration or security policy from normal judicial scrutiny for potential violations of the Bill of Rights. The First Amendment, like the rest of the Bill of Rights, was intended to limit government power generally; it wasn’t framed as applying to only specific categories of policies, or to certain categories of people.

While many argue that special deference to the executive in the immigration arena is mandated by previous Supreme Court precedent, the relevant cases are far less definitive than is often believed. And the court could have limited the scope of those precedents rather than give immigration policy near-total exemption from normal constraints on religious discrimination.

The executive certainly does sometimes have specialized expertise on immigration and national security. But the same is true of numerous other areas of government policy, where the courts do not apply any such ultradeferential approach, including even internal violence and terrorism. Almost any policy addressing a complicated issue is likely to have elements that involve specialized expertise.

Another possible distinction between the two cases (one not adopted by the court) is that much of the evidence of discriminatory intent in the travel ban case comes from campaign statements, rather than official deliberations. But there is no good justification to exempt campaign statements from consideration as evidence. Such statements can provide powerful indications of motive, and excluding them would create a dangerous road map for future discriminatory policies, allowing politicians to state their true purposes in the campaign only to use more guarded language later.

Finally, some suggest that Masterpiece Cakeshop presents a special situation because it involved a decision by “an adjudicatory body” (as Justice Anthony Kennedy put it in his majority opinion) rather than a legislature or an executive official. The Civil Rights Commission acted more like a court than like an elected official, in other words, and therefore perhaps should be subjected to tighter judicial scrutiny of its members’ statements.

But as Justice Kennedy also noted, the general rule at issue is that the government may not “base laws or regulations on hos­tility to a religion or religious viewpoint.” That rule applies to all branches of the government.

Ultimately, the different results in the two cases arise from the application of a double standard between immigration (especially when linked to national security) and other areas of government policy. In a concurring opinion in the travel ban case, Justice Kennedy shows some discomfort with this double standard. He suggests that there might still be some judicial review of immigration policies that are motivated by “animosity to a religion” — adding that even in situations where “statements and actions of Government officials are not subject to judicial scrutiny or intervention… officials are [not] free to disregard the Constitution and the rights it proclaims.”

Unfortunately, the effect of this case will be that unscrupulous officials are indeed largely “free to disregard the Constitution” in a critical area of government policy. That would not have been so if the Supreme Court had applied the same anti-discrimination standard in Masterpiece Cakeshop and the travel-ban case.

Ilya Somin is professor of law at George Mason University, author of Democracy and Political Ignorance: Why Smaller Government is Smarter. and an adjunct scholar at the Cato Institute. He co-wrote an amicus brief in the travel ban case on behalf of a group of constitutional law scholars.

The Big Idea is Vox’s home for smart discussion of the most important issues and ideas in politics, science, and culture — typically by outside contributors. If you have an idea for a piece, pitch us at thebigidea@vox.com