Certain genetic variants in babies who were exposed to opioids in the womb may protect them, leading to shorter hospital stays and less treatment, according to a new study.

Infants who had certain variations in two genes had less severe symptoms of withdrawal from the medications, compared with babies who did not have the variants, Bloomberg News reports. All of the babies in the study had neonatal abstinence syndrome, meaning they experienced drug withdrawal symptoms after being exposed to opioids. The mothers of the infants in the study had taken methadone or buprenorphine to treat addiction during their pregnancies.

The study is the first to examine the relationship between genetics and opioid withdrawal in infants, the researchers report in this week’s Journal of the American Medical Association. The results may help scientists find more effective ways to treat neonatal abstinence syndrome, said study author Jonathan Davis of Floating Hospital for Children at Tufts Medical Center in Boston.

“Our goal is to identify the highest-risk babies early, try to develop treatment strategies that will prevent some of the withdrawal and try to get the babies home sooner,” Davis told Bloomberg News. “These babies are so uncomfortable and in the hospital for such a long time, we’re talking millions and millions of dollars of health-care costs for something that we have to try to prevent.”

The study included 86 pairs of mothers and infants, who had their DNA analyzed. Babies with a variation of one gene were in the hospital 8.5 days fewer than those without the variation, and were less likely to need treatment. Babies with a variation in a second gene were in the hospital 10.8 fewer days, and needed less treatment than babies without the genetic change.

Both genes examined in the study, without variations, are linked with an increased risk of opioid addiction in adults, the article notes.