The U.S. Supreme Court on Wednesday dealt a blow to organized labor, ruling that non-members cannot be forced in certain states to pay fees to unions representing public employees such as teachers and police, shutting off a key union revenue source.

On a 5-4 vote powered by the court’s conservative majority, the justices overturned a 1977 Supreme Court precedent that had allowed the so-called agency fees that are collected from millions of non-union workers in lieu of union dues to fund non-political activities like collective bargaining.

The ruling means that the estimated 5 million non-union workers who pay these fees will no longer have to do so.

The court ruled that forcing non-members to pay agency fees to unions whose views they may oppose violates their rights to free speech and free association under the U.S. Constitution’s First Amendment.

Writing for the court, conservative Justice Samuel Alito said that while the ruling “may cause unions to experience unpleasant transition costs in the short term” that must be weighed against “how many billions of dollars have been taken from nonmembers and transferred to public-sector unions in violation of the First Amendment.”

The four liberal justices dissented.

The decision was issued on the final day of the court’s current term, which began in October, and came a day after the justices upheld President Donald Trump’s travel ban targeting several Muslim-majority nations.

Two dozen states require agency fees, and an estimated 5 million non-union workers for state and local governments pay them. Federal employees and private-sector workers do not.

Unions contend that mandatory agency fees are needed to eliminate the problem of what they call “free riders” - non-members who benefit from union representation, for example through salary and working conditions obtained in collective bargaining - without paying for it.

Conservative activists took aim at the fees as they sought to curb the influence of unions, which often support the Democratic Party and liberal causes.

The ruling deprives unions of a vital revenue stream, undercut their ability to attract new members and undermine their ability to spend in political races.

The plaintiff in the case is Mark Janus, a child-support specialist for the state of Illinois who opted not to join the union that represents employees like him, the American Federation of State, County and Municipal Employees (AFSCME).

A lower court had ruled against Janus, setting up the Supreme Court showdown.

The justices heard arguments in a similar case in 2016 involving non-union public school teachers in California, and appeared poised to overturn the 1977 precedent. But the death of conservative Justice Antonin Scalia weeks later left the court with an even split of conservatives and liberals, and its 4-4 decision in March 2016 failed to resolve the legal question.

Republican President Donald Trump’s appointment of Justice Neil Gorsuch last year restored the Supreme Court’s conservative majority, and Gorsuch’s vote proved to be crucial.

Janus was backed in the legal fight by anti-union groups including the National Right to Work Legal Defense Foundation and Liberty Justice Center. Janus has said he is not a member of a political party and his objection to the fees was not based on politics. Janus had been forced to pay just under $50 a month in agency fees since starting his current stint working for Illinois in 2007.

AFSCME and other public-sector unions have called the case a well-funded attack by corporations and billionaires to undermine organized labor.

Reporting by Lawrence Hurley; Additional reporting by Robert Iafolla; Editing by Will Dunham