WASHINGTON (AP) — The Supreme Court on Monday threw out mandatory life in prison without parole for juveniles. The ruling continued its trend of holding that children cannot be automatically punished the same way as criminal adults without considering their age and other factors.

The 5-4 decision split along ideological lines: The court’s four liberals and swing vote Justice Anthony Kennedy joined to order states and the federal government to allow judges and juries to consider a juvenile’s age when they hand down sentences for some of the harshest crimes, instead of making life in prison without parole automatic.

By making youth “irrelevant to imposition of that harshest prison sentence, such a scheme poses too great a risk of disproportionate punishment,” wrote Justice Elena Kagan, who was joined in the majority opinion by Kennedy and Justices Ruth Bader Ginsburg, Stephen Breyer and Sonia Sotomayor.

Monday’s decision left open the possibility that individual judges could sentence juveniles to life without parole in individual cases of murder, but said state and federal laws cannot automatically impose such a sentence.

This decision is in line with others the court has made, including ruling out the death penalty for juveniles and life without parole for young people whose crimes did not involve killing.

Dissenting, the court’s four conservatives said nothing in the Constitution forbids laws requiring mandatory life in prison without parole for juveniles. Chief Justice John Roberts was joined in the main dissent by Justices Antonin Scalia, Clarence Thomas and Samuel Alito. Alito also wrote separately and read his dissent aloud in the courtroom.

Under the majority opinion, Alito said, “Even a 17 1/2-year-old who sets off a bomb in crowded mall or guns down a dozen students and teachers is a ‘child’ and must be given a chance to persuade a judge to permit his release into society. Nothing in the Constitution supports this arrogation of legislative authority.”

Twenty-six states and the federal government have made life in prison without parole mandatory for some types of murder and allowed it to be applied to 14-year-olds, court papers said. In addition, Louisiana has some mandatory life without parole sentences for 15-year-olds, and Texas has some for 17-year-olds.

The decision came in the robbery and murder cases of Evan Miller and Kuntrell Jackson, who were 14 when they were convicted and sentenced to life without parole.

Jackson was sentenced in Arkansas after the shooting death of a store clerk during an attempted robbery in 1999. Another boy shot the clerk, but because Jackson was present he was convicted of capital murder and aggravated robbery.

Using his case, Breyer and Sotomayor said in a separate opinion they would have gone even further than just getting rid of laws requiring mandatory life in prison without parole for juveniles like Jackson. “There is no basis for imposing a sentence of life without parole upon a juvenile who did not himself kill or intend to kill,” Breyer said in a separate opinion.

Miller was convicted in Alabama of capital murder during the course of arson. A neighbor, while doing drugs and drinking with Miller and a 16-year-old boy, attacked Miller. Intoxicated, Miller and his friend beat the 52-year-old man and set fire to his home, killing him.

Monday’s high court ruling “is an important win for children,” said Bryan Stevenson, executive director of the Equal Justice Initiative, who represented Jackson and Miller. “The court took a significant step forward by recognizing the fundamental unfairness of mandatory death-in-prison sentences that don’t allow sentencers to consider the unique status of children and their potential for change. The court has recognized that children need additional attention and protection in the criminal justice system.”

Alabama Attorney General Luther Strange expressed disappointment with the decision. “It is rare that a juvenile commits the worst kind of murder. But when this happens, the Alabama Legislature and most other American legislatures have determined that the appropriate sentence is life without parole. And they have made that sentence mandatory. Thus, it is hard to understand the Court’s ruling that this sentencing procedure is ‘unusual’ for the purposes of the Eighth Amendment,” he said.

The court’s ruling was based on the Constitution’s Eighth Amendment prohibition against cruel and unusual punishment.

Strange said he didn’t expect the ruling to be applied retroactively to others whose sentences are already final. And while Miller will get another sentencing hearing, “the prosecution will have a compelling argument that he should receive a life-without-parole sentence,” he said.

In Arkansas, prosecutor Scott Ellington said he expected the state Supreme Court to notify prosecutors, lawyers for Jackson and any others affected by the ruling to tell them what happens next. “The speculation is that those cases that are affected in this case will have to have a new sentencing hearing,” Ellington said.

According to data provided to the court, roughly 2,500 people are behind bars for life with no chance of winning their freedom for murders they committed before their 18th birthday. More than 2,000 of them were there because the sentence was mandated by a legislature.

Advocates say 79 of them are in prison for crimes that took place when they were 14 or younger.