Sex offenders considered sexually dangerous can now be held in prison indefinitely after their prison terms are completed, the Supreme Court ruled on May 17.

According to the Associated Press, the Supreme Court in a 7-2 decision reversed a lower court ruling which held that Congress exceeded its authority in passing a law permitting indefinite detentions of those regarded as sexually dangerous, known as “civil commitment.”

“The statute is a ‘necessary and proper’ means of exercising the federal authority that permits Congress to create federal criminal laws, to punish their violation, to imprison violators, to provide appropriately for those imprisoned and to maintain the security of those who are not imprisoned by who may be affected by the federal imprisonment of others,” Justice Stephen Breyer wrote in the opinion for the majority.

The Adam Walsh Act, signed by George W. Bush in 2006, permitted the civil commitment of sexually dangerous prisoners. But four men, who were serving prison terms for sex crimes, were still detained after their sentences when prison officials said it would still be a risk of sexually violent conduct upon their release.

In a ruling by the 4th U.S. Circuit Court of Appeals in Richmond Va., it was declared that Congress had exceeded its authority when it held people indefinitely who were still considered sexual threats.

But “we conclude that the Constitution grants Congress legislative power sufficient to enact” this law, Breyer said, according to the AP.