Naps can be a great way to catch up on sleep—if you do them right. Wake up at the wrong stage of sleep, and you’ll feel groggy and terrible, not refreshed and perky. When your brain isn’t allowed to get through a full sleep cycle, you’re likely to experience what scientists call “sleep inertia,” or what regular folks might just call grogginess. Researchers are now able to tell us why our cognitive performance is so poor right when we wake up. In a new study that scanned the brains of volunteer nappers, PsyPost reports, scientists found that waking up from a nap disrupts the functional connectivity between brain networks, among other neurophysical changes.

The recent study published in the journal NeuroImage looked at the brains of 34 volunteers before and after they took a 45-minute nap. Researchers at the University of California, Berkeley and the Lyon Neuroscience Research Center in France measured the subjects’ brain activity five minutes and 25 minutes after waking up using EEG, fMRI, and behavioral observation (having them do mental subtraction) to see exactly how sleep inertia affects the body. The participants were asked to stay awake from 10 p.m. to 5 a.m. the night before to ensure they would fall asleep during the test. Some were woken up during relatively light sleep (stage 2 sleep) while others were woken up during deep sleep (stage 3 sleep).

They found that both groups showed decreased performance on the subtraction task. Right after they woke up, the participants’ EEG readings showed more delta brainwave activity (associated with deep sleep), and the fMRI scans showed decreased functional connectivity between brain networks. The brain connectivity disruption was worse for those awoken during the deep sleep stage.

By the 25-minute period, though, most of the effects on the brain had worn off, with participants’ brains returning to states similar to pre-test measurements. However, the light-sleep group seemed to recover better, while the sleep inertia didn’t dissipate as quickly for the deep-sleep group.

So yes, the dangers of waking up at the wrong sleep stage are real. The researchers tell PsyPost that people should limit their naps to 25 minutes or less—ensuring they don’t reach the deep sleep phase at all—or spend 90 minutes sleeping through an entire cycle.

[h/t PsyPost]