BOSTON (Reuters) - A Massachusetts man who prosecutors said developed a detailed plan while in state custody to kill former U.S. President Barack Obama in a sniper-like attack and then flee was sentenced on Wednesday to 37 months in prison.

Alex Hernandez, 32, was sentenced by U.S. District Judge Indira Talwani in Boston after pleading guilty in May to one count of threatening to kill and inflict bodily harm upon the president.

The prison term will run concurrently with an earlier sentence Hernandez is serving for a firearms violation, on which he has two years left.

Talwani said that while Hernandez had engaged in “somewhat chilling” conversations with an undercover federal agent related to the contemplated attack, he deserved credit for backing away from his threats.

His lawyer, Miriam Conrad, had fought against the five-year prison term prosecutors wanted, asking whether the threats were “just a fantasy of someone suffering from mental illness most of his life.”

Hernandez told an inmate with whom he attended Muslim religious services that he wanted to kill Obama, while serving time at Old Colony Correctional Center in Bridgewater, Massachusetts, in March 2015, according to prosecutors.

The inmate told authorities Hernandez claimed to be upset about how Muslims were treated in the United States and said he could acquire weapons once he got out of prison, prosecutors said.

After learning Hernandez wanted to obtain a fake passport so he could flee the country after the attack, federal agents decided to introduce him to an undercover agent posing as someone at an embassy who could help, they said.

Hernandez wrote letters to the agent, saying he was a “brother in faith, a martyr; and as a martyr I wish to fulfill Allah’s wishes and not to live among infidels,” prosecutors said.

Hernandez met with the agent twice in prison and told him he wanted to attack the White House and wanted to learn how to shoot “like a sniper,” prosecutors said.

He also told the agent he was learning how to make explosives that could be placed around government buildings to “create chaos,” part of what prosecutors said was a backup plan in case the initial attack failed.

Prosecutors said searches of Hernandez’s jail cell revealed a list of assassinated former U.S. presidents and images of the Sept. 11, 2001, attacks, Osama bin Laden and members of Islamic State.