For years, former congressman Aaron Schock has poured millions of dollars into a seemingly hopeless legal battle to stay out of prison on charges of public corruption, fraud, and theft of government funds. On Wednesday, Schock emerged almost completely victorious, striking a stunning deal to avoid facing trial for what once seemed like a straightforward case of a public official using elected office to line his own pockets.

The announcement that federal prosecutors were dropping all felony charges against Schock in exchange for payment of money owed to the IRS, appeared to halt the Illinois Republican’s free-fall from grace, leaving reporters, court-watchers, and advocates for ethics in government stunned. How did so clear-cut a case go so wrong for prosecutors—and so right for Schock?

According to former federal prosecutors, sources close to the case, and Schock’s legal team, the ex-congressman got off by a mixture of prosecutorial sloppiness, ambiguously worded corruption statutes, and an unwillingness by the U.S. attorney in Chicago to defend the actions of Schock’s initial prosecutors on appeal.

Schock’s attorneys hounded prosecutors and federal investigators for alleged misconduct throughout the case’s four-year history, from asking misleading questions during grand jury testimony to what they called “distasteful and offensive” inquiries into the ex-congressman’s sex life.

“The government has investigated nearly every facet of Mr. Schock’s professional, political, and personal life,” Schock’s defense attorneys wrote in a 2017 motion to dismiss the charges. “This even includes his sex life. It is no secret that there has long been speculative gossip in the media about Mr. Schock’s sexual orientation. For no apparent reason, the government has felt itself compelled to investigate this too.”

Federal investigators, too, were seen as having potentially overstepped in their pursuit of information about Schock’s alleged misuse of taxpayer funds. According to one source close to the case, investigators ordered a Schock staffer to steal documents from his congressional office, which a House of Representatives lawyer alleged amounted to “a solicitation of that employee to steal official records,” a potential violation of the law itself.

Paul Krieger, a partner at Krieger Kim & Lewin and former chief of the U.S. Attorney’s Office for the Southern District of New York’s complex frauds and cybercrime unit, called the outcome “unusual.”

“Obtaining a non-prosecution agreement after being indicted, particularly in such a high-profile manner, makes it seem quite likely that the office, in reviewing the strength of the case, found significant flaws” in both the evidence against Schock and how it was gathered, Krieger said.

Schock’s status as a famous former congressman raised the stakes of his prosecution, making any missteps by federal prosecutors all the more consequential.

“Any case involving allegations of public corruption or corrupt acts by a public official is going to be one where, when it proceeds to trial, every aspect of the government’s conduct is going to be extraordinarily scrutinized,” Brian Jacobs, a partner at Morvillo Abramowitz and former federal prosecutor of public corruption cases, told The Daily Beast. “In every one of those cases, the government’s conduct comes under scrutiny.”

“There were some issues early on with the case—it was a clear-cut case, he committed fraud, period. He clearly committed fraud, but the prosecution handled it, from the very beginning, in a very clumsy manner,” a source close to the case told The Daily Beast. “Everything from evidence gathering to the way that they handled witnesses that were under immunity agreements... it just raised questions about prosecutorial misconduct, and it didn’t have to.”

The U.S. attorney’s office defended the deal.

“We believe this agreement provides a sensible resolution,” Joseph Fitzpatrick, an assistant U.S. attorney in Chicago, said in a statement to The Daily Beast. “It’s a just result and provides the necessary public accountability.”

Even considering the difficulties of successfully prosecuting a public corruption case, Schock’s deal with federal prosecutors is expansive. In exchange for Schock’s payment of $110,000 in reimbursements—$67,956 to various campaign committees and $48,000 to the Internal Revenue Service—U.S. Attorney for the Northern District of Illinois John Lausch will defer prosecution of the 22 felony counts against Schock. If Schock avoids breaking the law for the following six months, the charges will be dropped.

Short of limited concessions mandated by the deal, in which Schock admitted that his actions “may have caused his campaign committees and the House to pay expenses that Schock knew or should have known were not properly charged,” the ex-congressman framed the deal as proof that he was a victim of overzealous prosecutors who bit off more than they could chew.

In a victory-lap appearance on MSNBC’s Morning Joe on Thursday, Schock called the deferred prosecution agreement “a complete repudiation of the case that was launched against me.”

“The outcome validates this case should have never been started in the first place,” Schock said, in a statement provided by his attorney. “It should not have taken four years, two U.S. Attorney’s offices, three judges and millions of dollars in costs to the taxpayers and myself.”

George Terwilliger, Schock’s attorney, was magnanimous in victory, crediting “reasonable prosecutors” for crafting the deal.

“The vast majority of federal prosecutors do the right thing for the right reasons—this was the wrong case for the wrong reasons and it is gratifying to me to help fix that,” Terwilliger said in a statement provided to The Daily Beast. “People should take heart that honest prosecutors acting responsibly here in Chicago did what they promised: they gave the case an objective fresh look.”

During his tenure in the House, Schock was lavished with the public attention that comes with being young, single, conventionally attractive, and a member of Congress.

Schock was only 27 when he was elected to represent Illinois’ 18th congressional district in 2008, and over the following six years, the first member of Congress born in the '80s sought to give the stodgy Republican Party an injection of youthful vitality. He was an avid Instagrammer. He trained shirtless with West Point cadets. His ripped physique earned him ink in both muscle mags and The New York Times’ Fashion & Style section (headline: “A Congressman’s Abs Garner Yeas”).

Schock’s high profile, however, didn’t come without its complications. A photo of the congressman sporting a purple-checkered shirt, tight white pants and a teal belt at a White House picnic became fodder for months of derision by Capitol Hill’s notoriously bitchy congressional staffers (he later claimed to have burned the belt).

Schock managed to parry the questions about his personal life until February 2015, when a Washington Post reporter stumbled upon Schock’s Downton Abbey-themed office in the Rayburn House Office Building, designed by Illinois interior decorator Euro Trash.

Politico then reported that the luxurious office was the latest in a long line of extravagant expenditures on five-star hotels in exotic locations, chartered private planes, and the company of Jonathon Link, a personal photographer who had formerly shot weddings in Waxahachie, Texas, before being brought on to the congressman’s staff. Using geotagging on Schock’s Instagram account, the Associated Press uncovered even more spending, much of it paid for with taxpayer and campaign funds, this time on massages and tickets to a Katy Perry concert.

Once Politico revealed that Schock had been reimbursed for mileage on a campaign car that amounted to more than the car in question had ever been driven, the congressman announced his resignation in March 2015.

“[T]he constant questions over the last six weeks have proven a great distraction that has made it too difficult for me to serve the people of the 18 th District with the high standards that they deserve and which I have set for myself,” Schock said at the time. “I have always sought to do what’s best for my constituents, and I thank them for the opportunity to serve.”

In 2016, Schock was indicted in two dozen felony counts, including wire fraud, mail fraud, theft of government funds, making false statements, filing false election reports, and filing false tax returns. If convicted on all counts, Schock faced up to a century in federal prison.

But the case was marked by numerous issues almost from its outset. Two judges left the case—one by recusal, one after alleged misconduct in an unrelated case—and the conduct of the initial U.S. attorneys prosecuting Schock in Springfield, Illinois, made the work of future prosecutors much more difficult, according to a source close to the case.

After Schock resigned, Link—the former photographer—was convinced by investigators to record his calls with the former congressman for potential use in the impending criminal case. Such prosecutorial probes into the nature of Link’s relationship with Schock infuriated the ex-congressman’s legal team.

“Jonathon was wired—he set Aaron up on a recorded call with the feds, trying to get him to say why he filed the false invoices,” the source close to the case told The Daily Beast. “The fact that the government used those kind of dumb witnesses is another part of the prosecution’s problem.”

The U.S. attorney’s office in Springfield declined to comment on the resolution of Schock’s case.

The Department of Justice eventually replaced the prosecutors, moving the case to the U.S. attorney’s office in Chicago, while Schock pushed to get the indictment thrown out, fighting all the way to the Supreme Court with failed arguments that the charges were based on House rules, in violation of constitutional separation of powers.

Asked whether Chicago prosecutors agreed to the deal in a bid to avoid having to defend Springfield office’s conduct, Fitzpatrick told The Daily Beast that “we conducted a thorough review of the case before proceeding” with the agreement.

The muddled language of public corruption statutes was likely no help to prosecutors, even with an apparent mountain of evidence.

“Appellate courts, and the Supreme Court, have grappled with the public corruption federal statutes,” said Carrie Cohen, a partner at the firm Morrison & Foerster’s white-collar defense group and a former prosecutor of public corruption cases. “Where do you draw the line, how do we prove bribery schemes?... Is it too vague for politicians to know when they’ve crossed a line, and when they’re crossing a line? And how do we measure it?”

“What can be unethical, what can be immoral, what can be behavior that violates disclosure rules, that behavior, while perhaps undesirable in a public official, is not necessarily a criminal act,” Cohen told The Daily Beast.

As the case dragged on for years, Schock built a life for himself in Los Angeles, where a source socially acquainted with the former congressman told The Daily Beast that he is living in a small Beverly Hills apartment, working on converting an office building near Los Angeles International Airport into a luxury hotel.

Schock slowly eased his way back onto social media in true Instagram fashion—pretending that one’s life is perfect and fabulous while a million fires rage out of control behind the camera. The ex-congressman, who was burning through more than $4 million in campaign funds for a high-quality legal team, posted stories of himself heli-skiing in British Columbia, visiting temples in India, and flexing shirtless after Barry’s Bootcamp classes in West Hollywood.

The ex-congressman’s lavish lifestyle while facing two dozen felony counts, ethics watchdogs told The Daily Beast, is evidence of a broken system.

“Aaron Schock’s ethical misconduct and illegal activity, including his blatant personal use of campaign funds, was one of the biggest scandals by a sitting member of Congress in recent memory,” said Jordan Libowitz, communications director at Citizens for Responsible Ethics in Washington. “It’s disappointing that he will seemingly face minimal personal accountability for his actions.”

With the cessation of the legal case against him, Schock declined to tell reporters on Wednesday whether he would seek office again, but said on Morning Joe that he looks forward to “more opportunities that I can explore” in the future.

“He literally goes to Barry’s in Venice and WeHo every day,” said a person friendly with Schock in Los Angeles. “I think he’s just living his life.”