HELENA, Mont. (AP) – The Montana Supreme Court ruled Thursday that medical marijuana providers should be paid for their services, but it clamped down on commercial sales of the drug by limiting providers to no more than three patients each.

The court’s decision upheld other provisions of a 2011 state law that represents one of the most significant rollbacks attempted by the 23 states and Washington, D.C., that allow marijuana to be used for medical purposes.

The law, meant to curb abuses that led to a flourishing marijuana industry in Montana five years ago, would have banned medical pot providers from receiving any compensation. The court said the sales ban would leave some patients with debilitating illnesses with no available source for the drug.

However, the court upheld the requirement that marijuana providers can take on a maximum of three patients each.

“The Legislature determined that placing a limit on the number of registered cardholders a provider may assist serves the objectives of keeping marijuana away from large-scale manufacturing operations, making it less appealing to major traffickers,” according to the majority opinion written by Justice Beth Baker.

The justices also upheld provisions of the law that ban medical marijuana advertising and create an automatic review for doctors who recommend the drug for more than 25 patients.

The Montana Cannabis Information Association sued after the Legislature passed the law. Association Attorney Jim Goetz called the ruling a setback that will likely drive people to seek marijuana on the black market.

“Yesterday, there were 23 states providing for either full recreational use or medical use of marijuana,” Goetz said. “Today, we have 22 1/2.”

Attorneys for the state argued that the Legislature had the authority to limit an industry that had gotten out of control.

“I am grateful to the justices for upholding the rule of law and recognizing that it’s the Legislature’s rightful purview to set state policy on this important issue,” Attorney General Tim Fox said in a statement.

Montana voters legalized medical marijuana in 2004, but its growth exploded in 2009 after a U.S. Department of Justice memo suggested prosecuting marijuana cases would not be a priority. By 2011, the number of registered users had grown to more than 30,000 in a state of 1 million people, and the number of providers topped 4,800.

A booming black-market industry emerged that led to widespread recreational use, state officials said. Then, federal authorities raided large medical marijuana providers and growing facilities across the state, effectively shutting down the industry and prompting the 2011 restrictions by state lawmakers.

The restrictions passed after then-Gov. Brian Schweitzer vetoed a bill in 2011 to outlaw medical marijuana.

The legal fight that ensued has gone to the state Supreme Court twice. Medical marijuana advocates argued that providers would disappear if they had to give away the drug for free, leaving vulnerable and ill patients who can’t grow their own without a way to get the drug.

District Judge James Reynolds blocked the restrictions as unconstitutional in 2011, and again in 2012, when the state’s high court ordered him to use a different standard of review.

Other provisions of the law already have gone into effect, such as stricter requirements to show a medical condition that merits placement on the medical marijuana registry. That contributed to a drop in the numbers and a significant decline in the number of people under 30 who claimed chronic pain as a qualifying condition.

Since then, the registry numbers have been creeping up again. As of January, there were 13,640 registered users and 471 providers in the state.