ALAMOGORDO, N.M. (AP) — U.S. Border Patrol agents say they rescued an immigrant who was stuck on top of a moving train in southern New Mexico.

Agency officials say agents on Thursday morning saw a man atop an oncoming train in Alamogordo making hand gestures indicating he was in distress.

The agents were able to contact the train’s engineer, who made an emergency stop.

Agents helped the 34-year-old man down and determined he had injuries to his ankle and ribcage.

He was taken to a nearby hospital and treated.

Agents learned the man is a Mexican national who had crossed the border illegally on Wednesday in El Paso. He initially injured himself trying to get onto the train there.

He is currently being detained for processing.