Grace and Matthew Huang are back home in the United States after a nearly two-year legal battle with authorities in Qatar, a happy end to a narrative that sometimes follows a predictable script: Americans are incarcerated abroad; agitation generates high-level political intervention; charges disappear as the U.S. citizens are allowed to leave.

The Huangs’s case, prompted by suspicions arising from the death of their 8-year-old daughter last year, contained some of these elements but turned more on the withering of the legal case against them, which ended with an appellate judge excoriating the prosecution.

In his ruling on Sunday, appellate Judge Abdul Rahman al-Sharafi painstakingly criticized the work of the prosecution, in what the New York Times described as “a highly unusual development in the Qatari judicial system, where prosecutors and the police are often heavily favored.”

“The defense offered plenty of proof that they are not guilty,” the judge said, according to the Times.

U.S. officials familiar with the case said Friday that “there was a lot of work done behind the scenes to ensure the judges understood the evidence,” a tactic the officials emphasized over applying high-level political pressure to win the Huangs’ freedom.

“The judge said it better than I ever could,” said one U.S. official, who spoke on condition of anonymity because of lingering diplomatic sensitivities. “It’s been horrible for the Huangs, but it’s sort of a success story that the Qatari legal system functioned as any good legal system should. We just wish it could have happened more quickly.”

The Huangs moved to Qatar from California in 2012 with their three children so that Matthew, an engineer, could oversee an infrastructure project related to the country’s plan to host the 2022 World Cup. Their daughter Gloria, adopted from Ghana after a traumatic early childhood, died suddenly last January, drawing concern from Qatari officials who suspected the child had been mistreated.

The Huangs were arrested and their two other children placed in an orphanage, based on an initial, ultimately discredited theory that Gloria had been starved to death as part of a plan to harvest her organs or conduct medical experiments. The Huangs were convicted on a lesser negligence charge and sentenced to three years in prison. The couple spent nearly a year in prison before being released on appeal; their sons were released from the orphanage and sent home to family in the United States.

The couple was represented by the David House Agency, a crisis management firm, and supported by the California Innocence Project, a legal services nonprofit that works to free people who are wrongfully convicted. Here’s how CIP describes what afflicted Gloria:

Prior to her adoption by the Huangs, Gloria was born into extreme poverty in Ghana and was not adopted until she was four years old. When she arrived in the United States she had Giardia, a parasitic condition that can be difficult to eradicate and can cause a nutritional problem — i.e., impair the body’s ability to absorb nutrients from food. She later tested positive for Vitamin D deficiency and had other unusual blood work that, in retrospect, indicated a continuing malabsorption problem. From time to time she would exhibit an eating disorder — common among children with backgrounds similar to hers — where she would refuse food for days at a time and then eat more than an adult. Other times she would eat food from the garbage even when she had healthy food available. Yet most of the time she was vibrant and seemingly healthy.
Although it is still not known what caused Gloria’s death, the medical and other evidence shows that Gloria clearly did not die of starvation.

Media accounts of the Huangs’ situation note the cultural complexities raised by international adoption and expatriate living. Officials in Qatar, which doesn’t allow adoption, suspected the couple of engaging in human trafficking. “I believe that authorities in Qatar suspected foul play because we are Asian and we adopted three children from Africa who are black,” Matthew Huang told Yahoo News’s Katie Couric.

Qatar has been a U.S. partner on issues including the conflict against the Islamic State and helping to broker the release of Sgt. Bowe Bergdahl from Taliban captivity, and American diplomats joined the effort to resolve the case. U.S. Secretary of State John Kerry weighed in during a tense endgame, when the Huangs encountered difficulties leaving the country even after their acquittal and were left in limbo for several days. Kerry called Qatari Foreign Minister Khalid bin Mohammed al-Attiyah to press for the couple’s speedy return to the United States. The Qatari embassy in Washington did not immediately respond to a request for comment.

U.S. Ambassador to Qatar Dana Shell Smith accompanied the couple to the airport in Doha, the Qatari capital, and made sure they got on the plane. She tweeted:

After they reached California on Thursday, Eric Volz, managing director of the David House Agency, sent word to the family’s supporters.