A U.S. District judge ruled Tuesday morning in favor of a $26 billion deal to merge Sprint with T-Mobile, clearing one of the final hurdles.

While the deal still can’t close until the California Public Utilities Commission approves the transaction, Tuesday’s ruling culminates a yearslong courtship between Sprint and T-Mobile, which have made multiple attempts over the years to merge, only to abandon their plans fearing regulatory scrutiny.

Attorneys general from New York, California, Connecticut, Hawaii, Illinois, Maryland, Michigan, Minnesota, Oregon, Wisconsin, Massachusetts, Pennsylvania, Virginia and Washington, D.C., originally brought the lawsuit to block the deal following approval from the Justice Department and the Federal Communications Commission.

The states had argued that combining the No. 3 and No. 4 U.S. carriers would limit competition and result in higher prices for consumers. The companies had argued their merger would help them compete against top players AT&T and Verizon and advance efforts to build a nationwide 5G network.

In his decision filed Tuesday, Judge Victor Marrero wrote, “The resulting stalemate leaves the Court lacking sufficiently impartial and objective ground on which to rely in basing a sound forecast of the likely competitive effects of a merger.”

The judge laid out three points on which the court rejected the states’ objections to the merger. First, he said, they failed to convince the court that the merged party “would pursue anti-competitive behavior that, soon after the merger, directly or indirectly, will yield higher prices or lower quality for wireless telecommunications services.”

Get breaking news and insider analysis on the rapidly changing world of media and technology right to your inbox. This site is protected by recaptcha

Second, the court rejected that Sprint would be able to continue operating effectively as a wireless services competitor without the merger.

“The Court is thus substantially persuaded that Sprint does not have a sustainable long-term competitive strategy and will in fact cease to be a truly national [mobile network operator],” the ruling said.

And finally, the court rejected the states’ argument that Dish Network “would not enter the wireless services market as a viable competitor nor live up to its commitments to build a national wireless network.” The deal called for Dish to step in as a new wireless player based on agreements with the DOJ and FCC.

In a statement following the ruling, New York Attorney General Letitia James, who helped lead the states’ push, said the states “disagree with this decision wholeheartedly, and will continue to fight the kind of consumer-harming megamergers our antitrust laws were designed to prevent.” She called the ruling a “loss” for Americans who rely on wireless networks and said the states will review their options, including a potential appeal.

“From the start, this merger has been about massive corporate profits over all else, and despite the companies’ false claims, this deal will endanger wireless subscribers where it hurts most: their wallets,” James said.

California Attorney General Xavier Becerra, who also led the states’ efforts, said in a statement, “Our fight to oppose this merger sends a strong message: Even in the face of powerful opposition, we won’t hesitate to stand up for consumers who deserve choice and fair prices. We’ll stand on the side of competition over megamergers, every time. And our coalition is prepared to fight as long as necessary to protect innovation and competitive costs.”

Top executives at T-Mobile and Sprint claimed a victory with the judge’s ruling.

″Now we are finally able to focus on the last steps to get this merger done!” T-Mobile CEO John Legere said in a statement.

T-Mobile and Sprint agreed to certain concessions to the government before the agencies cleared the deal. The companies told the FCC they would deploy a 5G network covering 97 percent of the U.S. population within three years of closing the deal. Sprint also agreed to sell Boost Mobile, Virgin Mobile and other prepaid phone businesses, as well as some of its wireless spectrum to Dish for $5 billion before gaining approval from the Justice Department.

FCC Chairman Ajit Pai said in a statement that he was “pleased” with the court’s ruling and that the merger “will help close the digital divide and secure United States leadership in 5G,” calling it “a big win for American consumers.”

If approved by the California commission, the deal would create a new wireless competitor in Dish, which has tried for years to become a provider, spending billions on airwaves it has stored away. Dish will instead focus its efforts on building a 5G network covering 20 percent of the country by June 2022 and 70 percent of the U.S. population by June 2023.

Legere announced last year he would step down from his role and be succeeded by President and COO Mike Sievert.