No one likes making mistakes on the job, but it’s easy to lose focus when you’re stuck doing the same thing over and over. What if you could predict — and prevent — such errors?

A new study shows that the brain begins to wander as long as thirty seconds before the body makes an error, a departure signaled by changes in the brain’s blood-flow patterns.

Tom Eichele of the University of Bergen in Norway, Stefan Debener of the Institute of Hearing Research in Southampton, England, and several colleagues used functional magnetic resonance imaging (fMRI) to monitor the brains of thirteen subjects as they undertook the “flanker task.” In that classic psychological test, subjects select one of two buttons depending on the direction of arrows displayed on a screen.

Analyzing the brain’s blood-flow patterns, the team found that a subject tended to blunder after the brain simultaneously activated a set of regions associated with rest, and reduced activity in a different area associated with staying on task.

Intriguingly, the change began up to half a minute before an error occurred, and the brain seemed to refocus after the subject caught the mistake.

The study challenges a long-standing theory that the brain flubs simple tasks because of fleeting, random errors in neuron firing. With the new information in hand, it may be possible to build a device that warns us when we’re drifting off.

The research was detailed in the Proceedings of the National Academy of Sciences.