MADISON, Wis. (AP) — Wisconsin regulators can impose operating conditions on factory farms and consider high-capacity wells’ cumulative environmental impacts when deciding whether to grant permits, the state Supreme Court ruled Thursday.

The decisions mark a major victory for conservationists and clarify that the Department of Natural Resources has broad authority to protect Wisconsin’s waters. The rulings cap a pair of fierce legal battles between industry associations and environmental groups that have raged for years.

“This is a huge win for Wisconsin and for anybody who cares about protecting our water,” said Evan Feinauer, staff attorney at Clean Wisconsin, one of the environmental groups that sued to force the rulings. “At the end of the day, this about our shared water resources and the people who rely on them.”

The factory farm case began in 2012 when Kinnard Farms in Kewaunee County asked the DNR for a water pollution permit that would allow it to add more than 3,000 cows, nearly doubling the size of its herd.

An administrative law judge ordered the DNR to impose conditions on the permit requiring the farm to monitor groundwater for contamination from manure and capping the number of animals.

The DNR refused to enforce the conditions, citing an opinion from then-Republican Attorney General Brad Schimel. He pointed to a state law Republicans authored in 2011 known as Act 21 that allows state agencies to take actions only if they’re specifically allowed in statute or administrative rule. The judges’ conditions weren’t laid out in statute, preventing the DNR from enforcing them, the agency argued.

Clean Wisconsin and Midwest Environmental Advocates sued in 2015 to force the department to impose the conditions. A state appeals courts sent the case directly to the Supreme Court.

The DNR, now under the control of Democratic Gov. Tony Evers, has changed its stance and now supports the permit conditions. But Republican legislators joined the lawsuit, siding with Kinnard Farms, saying if the DNR wanted to impose the conditions it would have to write them up as new administrative rules and try to win legislative approval.

The court ruled 4-2 that the DNR can enforce the conditions. Writing for the majority, Justice Jill Karofsky noted that other sections of state law grant the department “all authority necessary” to protect the state’s waters and allow the department to mandate conditions to achieve that goal. Nothing in state law requires the department to draft individual rules to assure compliance, she added.

Clean Wisconsin and the Pleasant Lake Management District filed a separate lawsuit in 2016 alleging the DNR should have considered the widespread impact of eight high-capacity wells before granting permits for the projects. Environmentalists contend that high-capacity wells are draining lakes and depleting groundwater levels across central Wisconsin.

Again, the DNR pointed to Act 21, saying nothing in state law or rule explicitly permits them to consider cumulative impacts when granting high-capacity well permits. The department approved all eight permits in question without any conditions. Republican legislators and a host of industry groups, including Wisconsin Manufacturers and Commerce, the state’s largest business group, joined the case.

The court ruled 4-2 on Thursday that the DNR can indeed consider cumulative impacts. Justice Rebecca Dallet, writing for the majority, mirrored Karofsky’s opinion in the farm case, saying the agency has broad authority under state law to protect state waters and that statutes explicitly state the DNR “shall formulate plans and programs” to that end.

“Great news,” Carl Sinderbrand, an attorney for the Pleasant Lake Management Lake District, said of the decision. “You continue to have an opportunity to go to DNR and say ‘you have a duty to protect my lake. You need to take action here.’”

Conservatives hold a 4-3 majority on the court but Chief Justice Annette Ziegler joined with liberals Karofsky, Dallet and Ann Walsh Bradley in a surprise move to form the majority. Conservative Justice Brian Hagedorn didn’t participate in the decisions.

The two remaining conservatives, Patience Roggensack and Rebecca Bradley, wrote in dissents that the majority opinion guts Act 21 and ignores the will of legislators who enacted it. Rebecca Bradley called the wells decision “a striking affront to the will of the people.”

Aides for Republican legislative leaders didn’t immediately respond to messages seeking comment Thursday. A Wisconsin Manufacturers and Commerce spokesman declined comment.