Looking for a natural way to get a better night’s sleep? If herbal teas and a warm soak aren’t doing it for you, it’s time to take a walk.

But you have to know exactly when to take that walk, or it might not help you sleep the way it could.

A little background: Brisk walking can not only make you sleep better at night, but can also help undo the fatigue and bad mood that often accompany insomnia, according to the National Sleep Foundation. Exercise also helps regulate your body’s circadian rhythms that promote sleeping and waking up at appropriate times.

So what’s the secret to walking to treat insomnia? Regularity and timing. You can’t be hit-or-miss about it. Here’s what to do…

How long to walk: Your goal should be 25 to 30 minutes a day of walking at a moderate-to-vigorous intensity. That amount can not only reduce insomnia but also boost your mood and give you more energy, according to research published in Journal of Sleep Research.

When to walk: For the biggest effect against insomnia, the National Sleep Foundation recommends exercising in the afternoon to early evening. For exercise to encourage sleep, you need it to raise your body temperature (which it automatically will do), but you also need to leave time for your temperature to fall afterward. Because the second part takes time, you don’t want to walk too close to bedtime. While nighttime exercise might still help if you simply can’t get in exercise earlier in the day, try not to walk (or do any kind of aerobic activity) in the two to three hours before you want to fall asleep, said Susan Besser, MD, a board-certified family physician with Mercy Personal Physicians in Baltimore. Walking in the morning can help you feel more alert during the day, helpful if you didn’t sleep well.

How often: Walk every day or as close to that as you can come. It’s also a plus if you can walk outside when the sun is shining, another helpful signal for those circadian rhythms.

How fast to walk: Moderate-to-vigorous intensity means walking at a brisk pace. You’ll know if you’re hitting that mark if you can’t sing or whistle while you’re walking, said Dr. Besser.

Bonus tip: Researchers at the Morehouse School of Medicine in Atlanta analyzed data from 7,000 older adults and found that those who were both socially active and walked for exercise were 40% less likely to have trouble falling or staying asleep. And there’s an easy way to kill both those birds with one stone—join a walking club or grab a few friends or coworkers and start your own.

Need more natural ideas for better sleep? Check out “What You Eat (Or Don’t Eat) Affects How You Sleep.”