After months of chaos, infighting, and high-profile departures, President Donald Trump has made a surprising addition to his legal team: former New York City Mayor Rudy Giuliani, who’s better known today as a rabidly pro-Trump TV personality than as a top-notch legal mind.

Giuliani, 73, has been one of Trump’s biggest public boosters and gave a keynote speech at the GOP convention after Trump won the Republican presidential nomination in 2016. He’s also regularly appeared on television to defend Trump against charges of collusion with Russia and other alleged misdeeds.

But despite early rumors that Trump might pick him to be the attorney general, Giuliani was never given a formal position in the administration. In his new job, Giuliani still won’t be a technical part of the White House, but he’ll have a formal role on Team Trump and serve as the president’s closest advisers.

He’ll be coming on at a pivotal and dangerous moment for the administration. Trump’s legal team has been decimated by a wave of departures just as special counsel Robert Mueller has markedly ramped up his investigation into Trump and many of his closest aides. Mueller recently gave information to federal prosecutors in New York on Trump’s close confidant Michael Cohen, which led to an April 9 raid of his offices, and has reached plea deals with four former Trump associates. He seems to be methodically building a potential obstruction of justice case against Trump.

For Giuliani, the immediate priority will be to help decide whether Trump should sit down for a high-stakes interview with Mueller. The most recent head of Trump’s legal team, John Dowd, resigned after Trump disregarded his warnings about the perils of such a meeting and insisted that he wanted to sit down with Mueller and his team.

There are also growing fears that Trump may move to fire Mueller or his boss, Deputy Attorney General Rod Rosenstein. For now, at least, Giuliani is striking a moderate tone and promising to work with the special counsel to negotiate an end to the investigation.

“I’m doing it because I hope we can negotiate an end to this for the good of the country and because I have high regard for the president and for Bob Mueller,” Giuliani told the Washington Post.

Giuliani is joining Trump’s team just as the president escalates a very bitter, and very public, fight with former FBI Director James Comey, whose new book, A Higher Loyalty, paints Trump as a liar who runs his administration like a mob boss

The former mayor will have a very personal reason for joining the White House broad and nasty attack on Comey: the fact the former FBI chief’s book blasts the former mayor almost as harshly as it blasts Trump.

Comey had ordered FBI agents to open a leak investigation into Giuliani

Comey and Giuliani have been battling since the fall of 2016, when the former mayor made a series of cryptic statements about an upcoming “surprise” just two days before Comey announced in late October 2016 that he was reopening his investigation into Hillary Clinton’s use of a private email server.

FBI investigators had found new emails on a computer used by Clinton’s aide, Huma Abedin, after Comey closed the investigation in July of that year. The FBI reviewed the new emails in a couple of days and closed the probe again before the election. Still, Clinton and her defenders have long argued that the Comey announcement was the decisive factor that helped Trump win the White House.

Giuliani later confirmed that his hints about a “surprise” were about Comey’s announcement and that FBI agents told him about the new trove of emails. However, Giuliani subsequently changed his story and said that Comey’s decision to reopen the case was a “complete surprise” to him.

Comey told the Senate Judiciary Committee in May 2017, less than a week before Trump fired him from his post at the FBI, that the agency was “looking into” where Giuliani might have gotten his information, though he didn’t mention the former mayor by name.

On Thursday night, Comey repeated that claim in an interview on MSNBC and said.

“I don’t know what the result of that was, I got fired before it was finished, but I know that I asked that it be investigated,” Comey said.

Left unsaid was how much of his book was devoted to blasting his former boss.

Comey hasn’t just gone after Trump. He’s been going after Giuliani, too.

That investigation wasn’t the first time that Comey had dealt with Giuliani.

Before he became mayor, Giuliani was a federal prosecutor in New York, running the Manhattan US attorney’s office where Comey worked.

Comey writes extensively about his impressions of Giuliani in his new book, but the overall theme is simple. Comey thinks Giuliani is a showman with an insatiable desire for public attention and acclaim.

“Rudy’s demeanor left a trail of resentment among the dozens of federal judges in Manhattan, many of whom had worked in that U.S. attorney’s office,” Comey wrote. “They thought he made the office about one person, himself, and used publicity about his cases as a way to foster his political ambitions rather than doing justice.”

Elsewhere in the book, Comey calls Giuliani’s leadership style “dangerous,” and says that it created “resentment” among judges towards the US attorney’s office, which Comey would go on to lead, for years.

Trump chose to hire Giuliani despite the FBI investigation and the former mayor’s controversial tenure as a federal prosecutor and mayor. The question now is whether Giuliani will prove capable of doing something his predecessors on Trump’s legal team couldn’t: figure out how to protect a moody, dishonest and impulsive client from the skilled prosecutors who seem to be drawing closer and closer to Trump and his inner circle.