Sometimes, such as when you are a parent to young children, there’s simply no way you’re going to get enough sleep. But just because your newborn was up most of the night or your toddler had trouble falling asleep, the rest of the world won’t expect any less of you — you still need to bring it at work and at home the next day. Luckily, a new study finds that there’s a foolproof way to improve your brainpower when you’re sleep-deprived.

For the study, published in the journal Physiology and Behavior, researchers from the University of Portsmouth in England had 12 participants do seven cognitive tasks on a normal amount of sleep, then sleep only five hours per night for three nights. They then had the participants do the seven tasks again while sleep-deprived and at rest, and again while cycling for 20 minutes.

They found that although each person’s performance on the tasks varied after being denied sleep — potentially because some people are more resilient to sleep deprivation than others — they all did better at the tasks when they were biking.

Their conclusion? If you need to use your brain on very little sleep, moderate-intensity exercise can help.

Notably, it might not make sense to do an extremely hard workout to boost brainpower while on little sleep. “If the exercise was any longer or harder it may have amplified the negative results and became a stressor itself,” study author Joe Costello, Ph.D., Associate Head for Research and Innovation within the School of Sport, Health and Exercise Science at the University of Portsmouth, said in a press release.

“One potential hypothesis for why exercise improves cognitive performance is related to the increase in cerebral blood flow and oxygenation,” Costello explained. To that end, the researchers performed a second experiment in which they had 12 participants do the seven cognitive tasks then pull an all-nighter, take the tests at rest, then again while cycling in low-oxygen conditions.

Even though they were deprived of oxygen during exercise, they still did better on the tasks while sleep-deprived and cycling than they did at rest, showing that something other than increased blood flow to the brain is causing the exercise-induced brain boost.

Other potential explanations include that exercise affects hormone regulation or other factors that affect cerebral blood flow, arousal, and motivation.

Limitations to the study include that all the participants were young and healthy, the vast majority were men, and several dropped out due to adverse events.

Sleep deprivation can take a massive toll on the body, in the short-term reducing cognitive performance, including attention span, judgment, and emotional state. In the long term, it can increase the risk of cardiovascular disease, obesity, neurodegenerative disorders, and depression. About 40% of people globally don’t get the recommended seven to nine hours of sleep per night.

And as Fatherly previously reported, if you’re sleep-deprived and have time to either take a nap or get a quick workout in, the better choice for your health is almost always exercise, unless you’re incredibly sleep-deprived.

“Movement is medicine for the body and the brain,” Costello said.