Human beings evolved to move. Our bodies, including our brains, were fine-tuned for endurance activities over millennia of stalking and chasing down prey. “We’ve engineered that out of our lives now,” says Charles Hillman, a psychology professor at Northeastern University who has spent decades studying the link between exercise and cognition. The toll our relatively new sedentary lifestyle takes on our bodies is clear: For the first time in U.S. history, younger generations are expected to live shorter, unhealthier lives than their parents.

While the myriad ways exercise can shape our bodies are well known, researchers have long suspected the same might be true of the brain. Decades of research have gone into examining the effect of exercise on attention, memory, and visual sensitivity, according to Richard Maddock, a professor of psychiatry at the University of California, Davis. “There is a very consistent finding that the brain works better after exercise,” Maddock says. But why that is has been harder to figure out.

“Few studies have really looked at what’s actually going on in the brain while we’re moving,” says Tom Bullock, a postdoc at the University of California, Santa Barbara. Only recently has technology given scientists the tools to zero in on the mechanisms at play. Aerobic exercise appears to lead to changes in both the structure of the brain and the way it operates, which together bolster learning in kids, give adults an edge on cognitive tasks, and protect against the cognitive declines that often come with age.

Here, we outline exactly what we know happens in your head when your heart rate rises.

Brain Waves Get a Boost

Your brain becomes much more active during exercise, “perhaps more active than at any other time,” says Maddock. One way neurons communicate is with electrical pulses, and sometimes entire networks of neurons fire in unison, like a group of soccer fans chanting together at a game. These synchronized pulses are known colloquially as brain waves. Different kinds of brain waves, characterized by the number of times they oscillate in a single second, are linked to one’s mental state and mood. Lower-frequency waves occur when we’re running on autopilot: brushing our teeth, driving, or sleeping, for example. Higher-frequency waves, known as beta waves, occur when we’re awake and mentally engaged and are associated with attention, memory, and information processing.

Using tools like an electroencephalogram (EEG), which pick up on these electrical pulses, researchers have found that aerobic exercise causes a shift in the amplitude and frequency of brain waves. More beta waves, in other words, means that exercisers may be in a more alert state. “The brain is in a different gear when the human being is in motion,” Maddock says.

You Become More Sensitive to the World Around You

During exercise, the brain becomes much more receptive to incoming information, leading to measurable changes in vision. Tom Bullock and Barry Giesbrecht, a professor of psychology and brain sciences at UC Santa Barbara, work in one of the few labs that have managed to measure the effects of aerobic activity on the visual cortex during exercise. Bullock says it’s taken him four years to figure out how to consistently and reliably record an EEG while a subject is in motion.

The visual cortex is designed to zero in on important features in the environment—the kind of features that might indicate, for example, the presence of a predator or prey—and filter out less important background noise. This year, Bullock and Giesbrecht found that low-intensity cycling boosted this feature-selectivity ability so the brain was able to better identify specific features during exercise.

Scientists have also administered cognitive tests right after exercise—for example, measuring the flicker fusion threshold (the rate at which a flashing light begins to look like it’s steadily glowing) and found the same thing: After exercise, one’s senses are heightened and thus can detect the flashing at a higher frequency than before exercise.

Taken together, these findings indicate that “people see more clearly and immediately after exercise,” Maddock says. “They can make finer visual distinctions; their perceptions are sharper.”

Your Brain Shores Up Neurotransmitter Stores

The benefits of exercise to your brain may begin as soon as your heart rate begins to rise. Imagine, if you will, climbing onto your bike for a morning ride and pedaling at a tough but sustainable clip. Your breath becomes faster and heavier as your lungs struggle to meet the oxygen demands of the body in motion. Your heart rate climbs as it pumps oxygenated blood around the body and into the brain. And in much the same way that your muscles demand more energy during exercise, the brain begins gobbling up glucose or other carbohydrates when the body is in motion.

“In the past, nobody had any idea what the brain was doing with all this fuel,” says Maddock. That is, until last year, when he and his colleagues published a new study in the Journal of Neuroscience. They discovered that the brain uses some of that fuel to build more neurotransmitters, the chemicals that relay messages around the nervous system. Maddock and his colleagues used MRI to measure levels of neurotransmitters in study subjects after a bout of exercise on a stationary bike and found that levels of glutamate and GABA—two of the most common neurotransmitters in the brain—had increased. The brain may be “filling up its stores of essential ingredients,” Maddock says. “Perhaps in order to deal with a sustained period of hunting, for example, or running or fleeing or war.” Exercise, in other words, may restock the brain with essential neurotransmitters that it needs to operate optimally.

This process might be why exercise has been shown to alleviate depression. Maddock’s team found that during activity, glutamate levels rise in the same region of the brain where stocks of the neurotransmitter have previously been found to be low in depressed patients.

Your Brain Becomes Younger

A few things happen in the exerciser’s brain that make the organ appear younger. First, studies in both animals and humans suggest that exercise sparks the production of growth factors that nourish new neurons and help existing cells survive. Budding neural cells also need more nutrients as they grow, and animal studies suggest that exercise promotes the release of other growth factors that promote blood vessel growth, which could deliver those nutrients. At least one study in humans has found that active individuals tend to have more and healthier blood vessels, or, in the words of the authors, a “younger-appearing brain.”

These structural changes in the brain generally take at least a few weeks to develop but lead to long-lasting improvements in regions of the brain associated with cognitive tasks, like working memory. “A lot of intervention studies that are out there show that aerobic exercise increases neurogenesis in the hippocampus, for example” says Giesbrecht. “The hippocampus is really critical for memory.”

Beyond that, research shows that aging exercisers have increased gray-matter volume in regions associated with general intelligence and executive function, which encompasses everything from attention to planning to problem-solving skills. Studies also show that fit adults have healthier white-matter tracts—the superhighways that connect various regions of gray matter—in the basal ganglia, a critical region for balance and coordination.

New Connections Between Neurons Emerge

Over time, exercise changes both the number of neurons in your brain and how they communicate. A 2016 study from the University of Arizona, for example, found that cross-country runners had increased connectivity between parts of the brain involved in memory, attention, decision-making, multitasking, and processing sensory information—the very same regions that tend to be hit hardest as we age—compared to healthy but sedentary controls. The networks that fire together as you run—coordinating your route, keeping tabs on traffic, trying not to trip on rocks, and maintaining your pace—strengthen as you use them, so that even at rest, runners tend to have greater connectivity between brain regions. It’s the kind of connectivity that musicians and cab drivers and other skills-based experts develop. At the same time, the runners had decreased connectivity with a region of the brain typically associated with mind wandering, which indicates runners may have increased focus or concentration skills.

So, Is Exercise Magic?

Hillman cautions that for now, exercisers should be realistic about what aerobic activities can do for the brain. “You shouldn’t expect to increase your IQ or anything of that nature,” he says. “We’re talking about small to moderate effects, which are potentially great for improving cognition and brain health.”

But Bullock and Giesbrecht envision a future in which doctors prescribe exercise instead of drugs. “Exercise is a potential prophylactic against some aspects of age-related cognitive decline,” Giesbrecht says. “When you think of the fact that we have an aging demographic and the high prevalence of depression, there might be simpler treatments out there, like exercise.”