Treatment for heroin addiction is most effective if it includes both inpatient and outpatient therapy, according to a new study.

Researchers at Boston Medical Center compared two groups of patients addicted to heroin: those who started buprenorphine treatment while in the hospital and then were referred directly to an outpatient buprenorphine treatment program, and patients who took a tapered dose of buprenorphine in the hospital to help with withdrawal, but only received referral information about local community treatment programs. Buprenorphine is an opioid substitute used to treat opioid addiction. It helps curb opioid withdrawal symptoms.

The study found 37 percent of patients in the group directed to the buprenorphine treatment program reported no illicit drug use in the month after leaving the hospital, compared with just 9 percent of those who only received general referral information, according to HealthDay. Patients in the outpatient treatment group reported fewer days of illicit drug use, and less drug use overall during the six months after they left the hospital.

The study of 139 patients appears in JAMA Internal Medicine.

“Unfortunately, referral to substance abuse treatment after discharge is often a secondary concern of physicians caring for hospitalized patients,” lead researcher Dr. Jane Liebschutz said in a news release. “However, our results show that we can have a marked impact on patient’s addiction by addressing it during their hospitalization.”