RALEIGH, N.C. (AP) — A North Carolina judicial panel on Tuesday rejected state legislative district maps, saying legislators took extreme advantage from drawing voting districts to help elect a maximum number of Republican lawmakers. The judges gave lawmakers two weeks to try again.

The three-judge panel of state trial judges unanimously ruled that courts can step in to decide when partisan advantage goes so far it diminishes democracy. Their ruling comes after the U.S. Supreme Court ruled in June in a separate case involving North Carolina’s congressional map that it’s not the job of federal courts to decide if boundaries are politically unfair, though state courts could consider whether gerrymandering stands up under state laws and constitutions.

The state judges found that the way the majority-Republican General Assembly redrew legislative district maps in 2017 violated the rights of Democratic voters under the state constitution’s equal protection and freedom of assembly clauses.

“Partisan intent predominated over all other redistricting criteria resulting in extreme partisan gerrymandered legislative maps,” the judges wrote in their ruling. “The effect of these carefully crafted partisan maps is that, in all but the most unusual election scenarios, the Republican Party will control a majority of both chambers of the General Assembly. In other words, the Court finds that in many election environments, it is the carefully crafted maps, and not the will of the voters, that dictate the election outcomes in a significant number of legislative districts and, ultimately, the majority control of the General Assembly.”

An appeal of the decision is expected and an ultimate ruling by the state Supreme Court could take months or longer. The high court has a 6-1 Democrat majority.

Rep. David Lewis, a chief defendant in the case, declined to comment.

The court gave lawmakers until Sept. 18 to again redraw maps.

Redistricting is the process of redrawing voting districts for state legislatures and Congress after every decennial census. Gerrymandering describes when the redistricting is slanted to give one political party a majority in as many districts as possible. North Carolina’s legislative districts were redrawn in 2017 after a federal court determined they were an illegal gerrymander that sought to weaken the voting strength of minority voters.

But the group Common Cause and more than 30 registered Democratic voters sued over the legislative maps, saying the districts were still so gerrymandered they unconstitutionally insulated Republicans from changes in voting behavior.

Four former governors — Democrats Jim Hunt, Mike Easley and Beverly Perdue, and Republican Jim Martin — also asked the three-judge Superior Court panel to nullify the districts.

Lawyers for North Carolina Republican legislators had argued there was no clear way for judges to know what kinds of map-making is unacceptable, since “redistricting is political because of what it is, not because of who does it.” Any complaints about how districts were drawn would vanish if Democrats could lead some GOP voters to change their minds and voted with them, GOP lawyers said.

If state courts ultimately rule in favor of Democrats and their allies, they could order new district maps for next year’s legislative elections. Lawmakers winning those elections will draw up maps after the 2020 census that are intended to last for the following decade, again influencing political power in the country’s ninth-largest state.

The lawsuit contended that most of the 170 House and Senate districts drawn in 2017 violated the plaintiffs’ free speech and association protections under the state constitution. It also said the boundaries violated a constitutional provision stating “all elections shall be free,” because the maps are rigged to predetermine electoral outcomes and virtually ensure Republican control of the legislature.

The three-judge panel agreed Tuesday that part of the state constitution was violated.

Evidence introduced during the two-week trial in July included computer records created by Tom Hofeller, a now-dead GOP redistricting guru who helped draw the 2017 legislative maps. Those files, collected by Hofeller’s estranged daughter after his death and shared with Common Cause, showed that Republican advantage was the overall objective of the latest redistricting.

Some of Hofeller’s files were also used in separate litigation in New York challenging a plan by President Donald Trump’s administration to include a citizenship question on the 2020 U.S. census.

The 2017 maps “do not permit voters to freely choose their representative, but rather representatives are choosing voters based upon sophisticated partisan sorting. It is not the free will of the People that is fairly ascertained through extreme partisan gerrymandering. Rather, it is the carefully crafted will of the map drawer that predominates,” the judges ruled.

The North Carolina lawsuit marks at least the eighth legal action challenging state election district maps since the current round of redistricting began in 2011. The lawsuits resulted in redrawing congressional lines in 2016 and legislative districts in 2017 — both to address racial bias.