FBI Director James Comey’s abrupt firing on Tuesday, executed by President Donald Trump at the recommendation of Deputy Attorney General Rod Rosenstein, is the latest stunning turn in a tumultuous season of American politics.

It is also a deeply ironic moment: Trump fired Comey for mishandling an investigation into Hillary Clinton’s emails. But while Trump claimed that Comey was too easy on Clinton, Rosenstein recommended firing Comey for improperly denigrating Clinton. And the firing allows Trump to replace an FBI director whom he never seemed to like, and who was overseeing an investigation that could be deeply damaging to the president, with one of his own choosing.

Trump’s defensiveness on this count is clear from the letter he sent to Comey informing him of the decision. “While I appreciate you informing me, on three separate occasions, that I am not under investigation, I nevertheless concur with the judgment of the Department of Justice that you are not able to effectively lead the Bureau,” Trump wrote. That line that seems calculated to inoculate himself against accusations that Comey’s firing was motivated by self-preservation—yet it unintentionally draws attention to the political dimension with its causal linkage.

Trump has repeatedly criticized Comey for being too soft on Clinton. During the campaign, he said she should be locked up and promised to prosecute her, despite Comey’s recommendation that Clinton not face charges for her use of a private email server and address while secretary of state. He has continued to criticize Comey since becoming president, most recently in a tweet last week:

Yet Rosenstein’s rationale for firing Comey was precisely the opposite: He concluded that the FBI director had badly overstepped his bounds in his handling of the Clinton case.

“I cannot defend the Director’s handling of the conclusion of the investigation of Secretary Clinton’s emails, and I do not understand his refusal to accept the nearly universal judgment that he was mistaken,” Rosenstein wrote in a letter to Attorney General Jeff Sessions.

Rosenstein identified several errors. First, he said Comey “usurp[ed]” the authority of Attorney General Loretta Lynch by announcing that he did not recommend charges against Clinton. Comey made that choice in part because he believed Lynch was compromised by a meeting with Bill Clinton shortly before the decision.

“Compounding the error, the Director ignored another longstanding principle: we do not hold press conferences to release derogatory information about the subject of a declined criminal prosecution,” Rosenstein wrote. In that press conference, Comey accused Clinton and her aides of being “extremely careless” with classified information.

Finally, Rosenstein said that Comey’s October 28, 2016, letter to Congress in which he announced the discovery of new emails related to the Clinton case was an error, and that his defense that not to write Congress would have been “concealing” something was wrong. On November 6, two days before the election, Comey followed up and informed Congress that the FBI had found no new evidence to change its recommendation. Clinton has blamed the October 28 letter for her loss on November 8, and some independent analysts, such as Nate Silver, agree.

Rosenstein wrote that he had conferred with a large group of former attorneys general and deputy attorneys general from both parties, stretching back to the Ford administration, and that nearly all of them shared his judgment that Comey had dangerously eroded traditions.  

It is ironic, then, that Trump—a president who has made manifest his disdain for traditions—has fired an FBI director who he didn’t like for breaking tradition in making a decision with which Trump disagreed. But while Trump may owe his presidency to Comey’s interference in the Clinton case, the FBI director had more recently become a thorn in his side. The FBI has been pursuing an investigation into Russian interference in the election, and whether Trump or aides colluded with the Russians. Comey’s disclosure of that investigation, on March 20, was a major blow to Trump, who insists that questions about Russia constitute a “charade.” Comey also debunked the president’s claim, offered without evidence, that President Obama “wiretapped” Trump during the campaign.

With his fierce independence—it’s worth recalling that Comey knew Clinton was the frontrunner in the election, and thus his likely future boss, when he criticized her—his bureau represented a threat to Trump that the president could not control. Comey had no intentions of leaving, telling a gathering in early March, “You’re stuck with me for another six and a half years.” (Comey is not the first FBI director to be fired: Bill Clinton dismissed William Sessions in 1993, while Louie B. Freeh was pressured to resign in 2001.)

It’s unclear who Trump might appoint to fill the director’s role, but Comey’s departure offers another chance for Trump, and his attorney general, Jeff Sessions, to powerfully reshape the Department of Justice. The new director will presumably be more closely aligned with the president. Directors serve a single 10-year term. Trump has also fired Acting Attorney General Sally Yates, who had previously been the deputy attorney general, in January, after she said the Department of Justice would not defend Trump’s travel ban, which she deemed unlawful. (Federal courts later agreed with her.) In March, Trump also fired Preet Bharara, the high-profile U.S. attorney for the Southern District of New York, where the Trump Organization is headquartered, despite having previously asked Bharara to remain in his role.

Earlier on Tuesday, the FBI sent a letter to the chair and ranking member of the Senate Judiciary Committee, noting that Comey had misstated some key facts about the October episode of the emails during a hearing last week. The letter came after several outlets reported on Comey’s error. During Tuesday’s White House briefing, Press Secretary Sean Spicer was asked whether Trump still had confidence in Comey, and said he had no reason to believe that Trump did not but had not talked to him.

During that Senate hearing last week, Comey was asked about his decisions on the Clinton case. “It makes me mildly nauseous to think that we might have had some impact on the election,” Comey said, but he added that he would make the right decision again. In another irony, his explanation for his actions was the same as one offered for firing him: Comey believed he was protecting the institution of the FBI, while Rosenstein and others concluded he was eroding them.

“I knew this would be disastrous for me personally, but I thought this is the best way to protect these institutions that we care so much about,” Comey said. He likely had no idea that his personal disaster was just beginning.