Halloween is nearly upon us, and that means parties, parades, pillowcases, and paranoia about people trying to poison our children. The annual media-fueled rumors of poisoned candy and razor blade–studded apples are unfounded, but there is one treat you might want to treat with extra caution: caramel apples.

From November 2014 to February 2015, at least 35 people from 12 states were infected with listeriosis. Of those people, 90 percent said they’d eaten prepackaged caramel apples before they got sick. A new study explains how it happened.

Listeriosis is an infection is caused by the bacterium Listeria monocytogenes. It mostly affects the elderly, pregnant women, and people with compromised immune systems, but in rare cases people without these risk factors can become infected. Early symptoms of listeriosis include fever, muscle aches, and diarrhea, but the infection often spreads from the gut into the rest of the body.

Once it becomes invasive, listeriosis can be deadly. All but one of the 35 people infected in last winter’s outbreak were hospitalized, and seven died, at least three of them as a direct result of the infection. Alarmed by the news, three brands of prepackaged caramel apples and one apple producer issued recalls.

But no one knew how the bacteria got into the apples in the first place. Under normal circumstances, caramel is too sludgy, and apples too acidic, for bacteria to grow. In a journal article published this week in mBio, researchers from the University of Wisconsin-Madison's Food Research Institute report two conditions that made the outbreak possible.

Bacteria that lands on the outside of an apple has usually reached a dead end. But inserting a stick into the apple breaks the skin and creates a tiny puddle of juice on the surface of the fruit. Smothering that sugary puddle in a layer of caramel then creates the perfect environment for bacterial growth.

The researchers also found that temperature plays a big role. They made caramel apples with and without sticks and swabbed them all with Listeria bacteria. Half of the apples went into the fridge and half were left on the counter. After three days at room temperature, the bacterial colonies on apples with sticks had multiplied by one thousand. Refrigerated apples with sticks were able to fend off bacteria for a week before succumbing. No bacteria at all grew on the chilled apples without sticks.

The solution here is pretty clear, say the researchers. You don’t have to give up on caramel apples. Buy them fresh, keep them in the fridge, and eat them within a few days.