A Virginia judge largely upheld a new state law that expands background checks to all gun purchases, following a challenge by gun rights groups that argued the requirement was unconstitutional.

Lynchburg Circuit Court Judge Patrick Yeatts ruled Tuesday that the act was “facially valid” and “facially constitutional,” but issued a narrow injunction blocking the law from applying to gun purchases made by 18- to 20-year-olds.

Yeatts found the law was unconstitutional for this subset of purchasers because federal law does not allow a federally licensed firearms dealer to sell handguns to or run a background check on someone under 21.

The new law, which went into effect on July 1, closed a loophole in Virginia law that allowed the private sales of firearms, including those made at gun shows, to take place without criminal background checks. Licensed dealers are required to perform such checks.

“Background checks save lives and they are supported by huge majorities of Virginians, including gun owners,” Virginia Attorney General Mark Herring (D) said in a statement.

The Gun Owners of America, Virginia Citizens Defense League, Gun Owners Foundation and three individuals filed a lawsuit in late June, seeking to block the law from going into effect.

One of the individuals named in the lawsuit wanted to sell guns to the other two after July 1, but the buyers didn’t want to have to pay a fee of about $17 to get a federal background check under the new law, according to the lawsuit.

An attorney for the plaintiffs did not immediately return a call for comment.

The measure was one of a handful of new gun restrictions passed by the Democratic-majority Virginia legislature during the past session and signed into law by Gov. Ralph Northam (D). Others limited individuals to purchasing one gun per month and included a “red flag law,” which allows authorities to temporarily take guns from people deemed a threat.