IZMIR, Turkey — An American pastor flew out of Turkey on Friday after a Turkish court convicted him of terror links but freed him from house arrest, removing a major irritant in ties between two NATO allies still strained by disagreements over Syria, Iran and a host of other issues.

The court near the western city of Izmir sentenced North Carolina native Andrew Brunson to just over three years in prison for allegedly helping terror groups, but let him go because the 50-year-old evangelical pastor had already spent nearly two years in detention. An earlier charge of espionage was dropped.

Hours later, Brunson was transported to Izmir’s airport and was flown out of Turkey, where he had lived for more than two decades. He was to be flown to the U.S. military hospital in Landstuhl, Germany, then on to Washington, where he was to meet with President Trump on Saturday.

“I love Jesus. I love Turkey,” an emotional Brunson, who had maintained he was innocent of all charges, told the court during Friday’s hearing. He tearfully hugged his wife, Norine Lyn, as he awaited the court decision.

“PASTOR BRUNSON JUST RELEASED. WILL BE HOME SOON!” Trump tweeted after the American was driven out of a Turkish prison in a convoy. Later, after Brunson was airborne, Trump told reporters the pastor had “suffered greatly” but was in “very good shape,” and that he would meet with him at the Oval Office.

Trump predicted at a campaign rally in Ohio that Brunson is “going to be in great shape.”

Brunson’s release was a diplomatic triumph for Trump, who is counting on the support of evangelical Christians for Republican candidates ahead of congressional elections in November.

Turkey could now hope that the U.S. will lift tariffs on Turkish steel and aluminum imports, injecting some confidence into an economy rattled by high inflation and a mountain of foreign currency debt.

Friday’s ruling followed witness testimony that seemed to partly undermine the prosecutor’s allegations and highlighted concerns that Turkey had been using the U.S. citizen as diplomatic leverage.