The Pakistani army changed commanders this weekend, putting a new man in the most important job in the country. General Raheel Sharif replaced General Ashfaq Kayani as Chief of Army Staff (COAS). The job has been the vantage point from whence all four of Pakistan’s military dictators took over the country; it oversees the fastest growing nuclear arsenal in the world and Pakistan’s network of connections to terrorist groups and the Afghan Taliban. The COAS also is America’s main interlocutor to the Pakistan army and the deep state it controls.

Kayani’s tour was due to expire, so the transition was months in the making. Kayani epitomizes the paradoxes and contradictions that are at the heart of Pakistan’s troubles today. A professional career army officer, he was selected by Pakistan’s last dictator, Pervez Musharraf, to run the country’s intelligence service in 2004 after a number of assassination attempts on Musharraf. As director general of the Inter Services Intelligence (ISI), Kayani was directly responsible for helping the Afghan Taliban recover from their defeat in Afghanistan in 2001 by American and allied forces.

By 2004 the Taliban had resumed the war inside Afghanistan. Pakistan gave it critical help and assistance. Without it, the Taliban would never have recovered. A secret NATO study leaked in 2012 based on the interrogations of 4,000 captured Taliban, al Qaeda, and other fighters in Afghanistan in over 27,000 interrogations concluded that ISI support was critical to the survival and revival of the Taliban after 2001. It provides sanctuary, training camps, expertise, and help with fund raising. Pakistani ISI officers have been killed on the battlefield in Afghanistan operating undercover with Taliban forces. The NATO report concluded, “the ISI is thoroughly aware of Taliban activities and the whereabouts of all senior Taliban personnel.”

Kayani ran the ISI’s covert operation assisting the Taliban directly until his promotion to COAS in 2007, when Musharraf’s regime began to fall apart. As DG/ISI, Kayani would also have been in charge of the early planning for the attack on Mumbai by the Pakistani terror group Lashkar e Tayyiba, which killed 166 people including six Americans in 2008. Kayani as the spymaster of ISI undoubtedly knew his organization had recruited an American, David Headley, to assist in doing the reconnaissance for the attack, which began in 2005.

Cooperation between the ISI and the Afghan Taliban and LeT continued after Kayani moved up, but in 2009 he confronted Pakistan’s own terror nightmare, the Pakistani Taliban. The Pakistani Taliban turned on the army’s deep state that had nourished and patronized jihadists for decades as too moderate and too willing to take America’s money. Kayani oversaw the army’s offensive into the Swat Valley that drove the Pakistan Taliban back into the border region with Afghanistan. Today the army is still engaged in a bloody and vicious war with the Pakistani Taliban even as it continues to assist the Afghan Taliban fight against America and NATO in Afghanistan and helps LeT plan more attacks in India.

I was with National Security Advisor Jim Jones in February 2009 when he confronted Kayani with the contradictions in Pakistani policy toward terrorism in his office in the West Wing of the White House. President Obama had just come into office and was reviewing policy toward Pakistan. Jones asked Kayani to explain his complicated relations with jihadist groups. Kayani mumbled a reply and changed the subject to Pakistan’s enemy India.

Obama chose not to tell Kayani that the CIA had found Osama bin Laden hiding less than a mile from the front door of the Kakul military academy, Pakistan’s West Point, in 2011. It was a remarkable decision since the United States had provided Pakistan over $25 billion in military and economic aid since 9/11 precisely to fight al Qaeda. At the moment of truth in U.S.-Pakistan relations, Obama rightly decided he could not trust Kayani.

The COAS also runs Pakistan’s nuclear program. Today it is probably the fourth largest nuclear arsenal in the world. Under Kayani’s watch, Pakistan has embarked on an ambitious program to develop and deploy tactical nuclear weapons that can be used on a battlefield. Pakistani officials argue that the development of these weapons is essential to confront India‘s conventional military superiority. It also significantly increases both the security risks to maintaining control of nukes and lowers the threshold to use them in a future conflict.

America’s dysfunctional relationship with Pakistan continued to go downhill even in the last days of the Kayani era. After a drone strike killed the head of the Pakistani Taliban, the country’s number one terrorist threat, many Pakistani politicians demanded the strikes cease for good. One party outed the top American intelligence officer in Islamabad and demanded he and CIA Director John Brennan be put on trial for the drone attacks. It’s the third time since 2010 that the CIA’s man has been outed in Pakistan.

Prime Minister Nawaz Sharif took great care in picking Kayani’s successor. The last time Sharif was prime minister, in 1999, he picked Musharraf to replace another general in the COAS position. Within months Musharraf had started a war with India in Kashmir and was taking the country to the brink of Armageddon. When Sharif tried to fire Musharraf, the army overthrew Sharif instead and he spent a decade in exile. Now in a twist of fate Musharraf is on trial for treason. Sharif is not the only Pakistani civilian leader who chose a COAS poorly. Zuflikar Bhutto chose General Zia ul Huq to be COAS in the 70s. Zia overthrew Zulfi and had him executed in 1977.

Sharif’s choice now, General Raheel Sharif, is not a relative. Born in 1956, Raheel comes from a military family. His elder brother died in the 1971 war with India and is a national hero. He is a graduate of the Kakul academy and was also its commandant at one point in his career. He was apparently not Kayani’s top choice for his successor but is likely to continue the army’s policies on backing terror and building nukes. Whether Prime Minister Sharif has chosen better than he did in 1999, of course, remains to be seen.