A man from Uzbekistan who pleaded guilty to plotting to kill US president Barack Obama with an automatic rifle claimed he was acting at the direction of an Islamic terror group in his home country.
Authorities said Ulugbek Kodirov had discussed trying to kill the president as he campaigned for re-election because he would be out in public more often.
Kodirov entered the plea during a hearing in Birmingham, Alabama before US District Judge Abdul K. Kallon, an Obama appointee.
Defence attorney Lance Bell said the 22-year-old Kodirov avoided a potential life sentence by pleading guilty.
He faces up to 30 years in prison, though Mr Bell expected Kodirov to receive about half that. The judge also told Kodirov that he will face deportation once he is released from prison.
Kodirov pleaded guilty to three counts: Threatening to kill the president, possessing an automatic weapon, and providing material support to terrorists. Four other charges were dropped as part of the deal.
Muslims in the Birmingham area who knew Kodirov were stunned to learn of his plans.
"I really didn't want to believe it," said Ashfaq Taufique, president of the Birmingham Islamic Society.
"I knew him. He attended mosque. He never demonstrated any radicalisation in his behaviour, his words."
US attorney Joyce Vance said members of the Islamic community assisted in the case against Kodirov.
The plea agreement says that in July 2011 Kodirov began communicating with a person known as "the Emir".
Kodirov said the person was a member of the Islamic Movement of Uzbekistan, which has been designated a terrorist organisation by the US State Department. The agreement does not reveal the identity of "the Emir" or say whether the government believes he actually is an Uzbek terrorist.
The Emir "asked Kodirov if there was anything Kodirov could do about President Obama since Kodirov was closer geographically to the president than the Emir".
Kodirov and the person discussed possible ways to kill Obama, including from long distance using a sniper rifle.
The agreement says Kodirov became disheartened when he realised how expensive sniper rifles are and realised he lacked the skill to pull off the shot. Kodirov then decided he could shoot the president from a closer distance in public.
"Kodirov said that he did not care if he got shot and killed, as long as he killed President Obama," according to the plea deal.
Kodirov then struck up a friendship with another person in Birmingham who spoke Uzbek, and the two often attended mosque together. The two often looked at jihadist websites and videos on Kodirov's laptop, the agreement says.
On July 11, after the two went to a mosque in Birmingham to pray, Kodirov asked his friend to buy a gun for him so he could kill Mr Obama.
Kodirov told the person he "knew this was what he was supposed to do for Islam", the plea agreement says.
Mr Bell, the defence lawyer, said Kodirov regrets what happened and "accepted responsibility for the charges he pled guilty to".
Kodirov, who was arrested in July, was accused of making four separate threats against Mr Obama within a five-day period when he was meeting either with a witness who went to police or an undercover officer.
Ms Vance, the prosecutor, said Kodirov's threats were serious enough that law enforcement officers felt they had to intervene.
"He had developed a plan, he was reaching out to other individuals for aid and acquiring firepower necessary to kill the president," Ms Vance said.