Many people view retirement as a time to stop working. But new research shows that people who take on full- or part-time jobs after retirement have better health.

The finding is based on data collected from 12,189 men and women over a 6-year period. The participants, who were from ages 51 to 61 at the start of the study, answered questions about their employment history, experiences after retirement and their physical and mental health.

Researchers from the University of Maryland found that men and women who kept working after retirement had fewer major diseases or disabilities than those who quit work, according to the study published this month in the Journal of Occupational Health Psychology. Retirees benefited whether the work was a full- or part-time job, self-employment or temporary.

Doctors have long known that staying active during the older years is associated with better health. But the question is whether working keeps older people healthy, or whether the people who continue working are able to do so because they were healthier to start with. But the new study controlled for health before retirement and found that post-retirement work had a distinct effect on health. Notably, the hours a person worked didn’t matter, showing that both part-time and full-time employment are beneficial after retirement.

While working after retirement is good for you, the data also suggest that retirees shouldn’t take on just any job. Among those who kept working, the retirees who found work related to their previous careers had the best mental health. The study authors speculated that working outside a person’s main area of expertise might lead to more stress, explaining the lower mental health for people who worked outside their main career field. Switching careers after retirement may also be a sign that the person had to find some form of work to cope with financial problems,

Other studies have also supported the idea that working after retirement is good for you. A long-term study of 1,000 men and women born in 1920 suggested that working after retirement was associated with living longer. The participants joined the study at age 70 and were tracked for 14 years by geriatrics researchers from the Hadassah Hospital Mount Scopus in Jerusalem.

After controlling for individuals’ health at the beginning of the study, the researchers found that whether a person was still alive after 12 years was strongly associated with whether they had been actively working or were fully retired. Among the 1,000 people studied, those who continued to work at age 70 and beyond were 2.5 times as likely to be alive at age 82 as those who had retired and were not working at the beginning of the study.

Another study showed that losing a job at an older age can be devastating to health. Yale researchers followed 4,220 workers, ages 51 to 61, for 6 years. During the study period, 457 workers lost their jobs. Being laid off close to retirement increased the risk for stroke by three times, according to the study published in the American Journal of Industrial Medicine.