After severing ties with Iran — following the torching of the Saudi embassy in Tehran by protesters angered by the execution of a dissident Shi‘ite Saudi cleric — Saudi Arabia is courting Pakistan’s support in its widening dispute with its long-standing regional rival.

In their first foreign trips this year, Adel al-Jubeir, the Saudi Foreign Minister, and Prince Mohammed bin Salman, the deputy crown prince and Defense Minister, traveled to Islamabad last week, within days of each other, meeting civilian leadership but also, crucially, Pakistan’s powerful generals.

Since announcing a 34-country “Islamic military alliance” last November, Saudi Arabia has been seeking the inclusion of the Muslim world’s second most populous country and sole nuclear power. The Pakistanis said they were initially surprised to learn of the coalition, but a Foreign Ministry spokesman reluctantly acknowledged Islamabad’s membership.

A senior Pakistani official also told TIME, “We are part of the coalition, but we will only be acting in our national interest.”

Following meetings with Prince Mohammed bin Salman on Sunday, the Pakistan army issued a statement asserting “that any threat to Saudi Arabia’s territorial integrity would evoke a strong response from Pakistan.” Pakistan’s Prime Minister Nawaz Sharif struck a more conciliatory tone, suggesting that Islamabad was willing to play the role of mediator between Saudi Arabia and Iran.

Pakistan, which is thought to be home to both the world’s second largest Sunni and Shi‘ite populations, fears inviting the Middle East’s sectarianism to South Asia.

Last month, a terrorist cell of Iraq-trained ISIS members was discovered in the major industrial town of Sialkot. Meanwhile, the Iranian Revolutionary Guard has been recruiting Pakistani Shi‘ites to join its fight against ISIS in Syria.

Following the Jan. 2 execution of Sheikh Nimr al-Nimr, the dissident Saudi Shi‘ite cleric, one of the largest Shi‘ite demonstrations anywhere took place in Pakistan’s largest city, Karachi.

These developments add to a plethora of security troubles Pakistan already has to deal with, including a domestic sectarian insurgency that has taken the lives of thousands of Pakistani Shi‘ites across all four of the country’s provinces over recent years.

Following outrage at the December 2014 massacre of over 100 children at a school in Peshawar, Pakistan has intensified its fight against militants at home, though human-rights groups criticize it for too eagerly favoring military courts, extrajudicial assassinations and executions, including those of prisoners who were convicted as juveniles.

The renewed campaign against militants has yielded results, however, with 2015 seeing a 48% fall in the number of terrorist attacks, year on year, according to the respected Pakistan Institute for Peace Studies. The same report, however, noted that there had been a 7% rise in the number of sectarian attacks.

For Saudi Arabia, Pakistan holds obvious appeal. It is a Sunni-majority country that can act as a nuclear-armed counterweight immediately to Iran’s east. Pakistan has a long history of dispatching its soldiers to protect the desert kingdom, garrisoning thousands of troops there in the 1980s and during the 1991 U.S.-led Gulf War.

The two countries also collaborated closely during the anti-Soviet war in Afghanistan, and Saudi Arabia has fitfully bailed out Pakistan’s constantly troubled economy. In 2013, it proffered a “gift” of $1.5 billion to ease Pakistan’s balance of payments crisis, a gesture the Saudis hoped would be repaid down the line. Saudi Arabia is also a vital source of remittances, with 1.5 million Pakistanis working in the kingdom.

But last April, Pakistan’s Parliament unanimously voted to decline a Saudi request to participate in its coalition fighting in Yemen against the allegedly Iranian-backed Houthi rebels. At the time, the Pakistanis said they were overstretched at home and unwilling to pick sides between a “brotherly” Saudi Arabia and a “neighborly” Iran.

That rebuff strained relations for the first time between the two countries. The Saudis’ anxieties about Iran’s nuclear ambitions had been partially offset by the knowledge that Pakistan, hitherto a close ally, boasted a growing stockpile of its own nuclear weapons.

Last November, Pakistan’s army chief, General Raheel Sharif, traveled to Saudi Arabia to soothe frayed nerves. He left with an agreement that Pakistan would remain committed to protecting the “two holy cities of Mecca and Medina” and “the territorial integrity” of Saudi Arabia. At the same time, Pakistan hopes to limit its commitment to those narrowly defined terms.

Pakistan’s relations with Iran have been fraught since the 1979 Islamic revolution swept Ayatullah Ruhollah Khomeini to power and Tehran drifted closer to New Delhi. In recent years, however, Pakistan has been trying to relieve its regional isolation by improving ties with Iran, looking to import 3,000MW of electricity across the border the two countries share to ease Pakistan’s crippling energy crisis. Last April, the Chinese government agreed to fund a gas pipeline between the two countries.

At the moment, with oil prices at a low, Iran is more important to Pakistan’s energy needs than Saudi Arabia. And when it comes to ending the war in Afghanistan, where Pakistan hopes to use its contacts with the Afghan Taliban to broker a political settlement, it wants the cooperation of both Saudi Arabia and Iran. Walking the tightrope between the Middle East’s two big rivals will prove a difficult balancing act.

Get The Brief. Sign up to receive the top stories you need to know right now. Please enter a valid email address. Sign Up Now Check the box if you do not wish to receive promotional offers via email from TIME. You can unsubscribe at any time. By signing up you are agreeing to our Terms of Use and Privacy Policy . This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply. Thank you! For your security, we've sent a confirmation email to the address you entered. Click the link to confirm your subscription and begin receiving our newsletters. If you don't get the confirmation within 10 minutes, please check your spam folder.

Contact us at letters@time.com.