Trouble sleeping? You’re not alone. The World Sleep Society says sleep problems are a “global epidemic,” threatening the health of “up to 45% of the world’s population.”

And it’s true, poor sleep quality affects our health in the long and short term. The day after a bad night’s sleep we may have trouble focusing, remembering, and learning. But years of sleep deprivation are associated with serious health problems, such as diabetes, a weakened immune system, and even some cancers. A persistent lack of sleep also negatively impacts our mental health and has been linked to depression, anxiety, and psychosis.

That’s why it’s crucial to exercise regularly in order to improve the quality of your sleep. Or is it the other way around? Does sleeping better make it easier to exercise regularly? Experts believe that sleep and exercise have a bidirectional relationship. Exercise helps us sleep better, and sleeping better gives us more energy to exercise during the day.

How does exercise improve sleep?

Exercise improves sleep in both direct and indirect ways. Most directly, moderate-to-vigorous exercise has been shown to improve sleep quality by lessening the time it takes to fall asleep. The “Sleep in America” survey conducted in 2013 found that roughly 76-83% of respondents who exercised regularly reported getting very good or fairly good sleep. Only 56% of non-exercisers said the same.

Indirectly, regular physical activity makes it easier to maintain a healthy weight, which reduces the risk of sleep problems associated with excessive weight gain. According to the American Academy of Sleep Medicine, roughly 60% of moderate to severe obstructive sleep apnea cases are attributed to obesity.

How does sleep improve exercise?

As the bidirectional relationship between sleep and exercise would suggest, research shows that people with poor sleep health are less physically active during the day. This is especially true for individuals with sleep disorders, such as insomnia or obstructive sleep apnea.

However, some research suggests that even one night of bad sleep can negatively impact physical activity the next day. One study found that when sleep was delayed by just thirty minutes participants exercised for one minute less the next day. That might not sound like a big difference, but when sleep is delayed for hours, the minutes of lost physical activity quickly add up.

Should you exercise before bed?

The jury is still out on whether or not exercising before bed hinders sleep quality. According to the Sleep Foundation, “traditional sleep hygiene dictates that intensive exercise during the three-hour period leading up to sleep can negatively impact sleep because it can increase your heart rate, body temperature, and adrenaline levels.”

Meanwhile, other survey results have found that the majority of people who exercise at 8 PM or later fall asleep quickly, experience adequate deep sleep, and wake up feeling rested. These results suggest that evening and late-night exercise might actually be the best option for some people.

With no real verdict on whether or not to avoid exercise before bed, experts recommend choosing your exercise schedule based on what best suits your individual needs. (Although exercising outside in the morning has been shown to improve sleep quality.)

Exercise will help you sleep better, and sleeping better will make it easier to stick with your exercise routine. The rest is up to you.