Robert Satloff is executive director of the Washington Institute for Near East Policy.

The Egyptian army’s removal of President Mohammad Morsi gives the Obama administration that rarest of opportunities in foreign policy: a second chance. Getting it right will require understanding where we went wrong the first time.

To some, President Obama’s fundamental error in Egypt policy was to withdraw U.S. support from longtime leader Hosni Mubarak in February 2011, when thousands of Egyptians first filled Tahrir Square demanding change. In this view, the United States should have stuck by Mubarak, a firm opponent of the Muslim Brotherhood whose soldiers fought alongside U.S. troops in the Persian Gulf war and who remained faithful to Egypt’s peace with Israel despite isolation in the Arab world.

That view, however, is wrong. Mubarak’s failing health meant that his rule was coming to an end, a fact he refused to face, instead surrounding himself with sycophants and acting like a modern pharoah. To U.S. interests, Mubarak had become a liability, and there was a reasonable alternative.

In early 2011, that reasonable alternative was the army. Despite Obama’s soaring rhetoric about the Tahrir activists being the inheritors of Gandhi and King, U.S. policy was effectively to protect American interests in Egypt by supporting the transfer of power from an 82-year-old air force general to a 75-year-old army general, Field Marshal Mohammad Hussein Tantawi, the leader of the Supreme Council of the Armed Forces. Egypt’s army, the country’s most popular institution, could be counted on to navigate political storms and find a civilian captain to steer the ship of state to a safe harbor — or so we thought.

In retrospect, the United States’ great error was born in the over-confidence we placed in the generals. When the White House moved on to Libya and other issues, it effectively put Egypt policy on auto-pilot, deferring to the generals on the pace and content of their “democratic transition.” Everyone knew the generals’ real interest was devising a political system that preserved their power and wealth. But they were “our guys,” men who understood the overlap of U.S. and Egyptian regional security imperatives — keeping the peace with Israel — and they claimed to appreciate the need for a political process that was popular, participatory and legitimate.

This policy was logical. And because no one imagined the breadth of the generals’ political incompetence, it was tragically wrong.

In just 18 months, the all-powerful Tantawi supervised a political process in which a marginal player in the Tahrir Square drama — the Muslim Brotherhood, a secretive, ideological Islamist underground movement — methodically engineered a takeover of Egypt’s political system. Tantawi may have let this happen believing he had found a domestic political player to safeguard the generals’ institutional interests, but reality soon set in. The Brotherhood’s takeover — partly through the ballot box, partly through extralegal means — was so complete that, in August 2012, one of the first moves of its newly elected president was to fire Tantawi. In a region of inept politicians, Tantawi stands out.

Throughout this period, the United States saw the generals make mistake after mistake — including failing to draft a constitution, blessing an elections law designed to favor Islamists and giving vent to anti-Christian violence — but policymakers did little more than pose questions. In public, we often advanced the right principles — pluralism, religious freedom, economic reform — but we consistently deferred to the generals in private. Even when the transition took an anti-American turn in late 2011, with the arrest and trial of American employees of U.S. government-funded pro-democracy institutions, Washington was embarrassingly quiet.

Thus, the pattern was set for U.S. relations with the Muslim Brotherhood and its representative in the presidential palace, Morsi. Like the generals, the Brotherhood promised a measure of stability, including a frigid peace with Israel. All it asked was to be left alone. At a time when “nation building at home” was the new American motif, the offer from the Egyptian people’s newly elected representatives was alluring.

The administration may have talked the talk of economic reform, but U.S. emissaries went to Cairo with checks, as though the money would keep flowing no matter how ill-conceived the Brotherhood’s populist economic policies. The administration pronounced on civil rights and tolerance, but it did nothing when journalists, activists and ordinary citizens were jailed for blaspheming Islam or insulting the president. And there can be no greater riposte to the American retreat in support of civil society, reflected in our spinelessness in the face of the prosecution of U.S. pro-democracy activists, than the millions of Egyptians who have filled streets in recent days to take back their country.

No one should revel in the deposition of an elected leader by a country’s military, but this is not a coup in the traditional sense and does not merit a suspension of U.S. assistance our law prescribes. Indeed, the army almost surely prevented a bloodbath that would have scarred Egypt for decades. Inaction, not intervention, would have been criminal.

But with the army back in the saddle, it would also be criminal for the administration to repeat the errors of the last episode of military rule, when everything was sacrificed on the altar of stability. Gen. Abdel Fatah al-Sissi, the new white knight, is no more a democrat than Tantawi, though he may be more adept. Washington needs to be more adept, too.

Obama has frequently said that the United States rejects the false choice between democracy or stability; nowhere is this more apropos than Egypt. In the post-Morsi world, Washington should articulate a policy in which our support for Egypt is conditioned not just on Egypt’s peace with Israel but also on continual movement toward building a democratic, pluralistic government that pursues sound economic policies. In practice, that means engaging broadly with Egypt’s political spectrum, not just the ruling party; offering vocal defense of minorities (Christians, Bahai’s, Shiites); reinvesting in civil society programs that help Egyptians translate their street activism into political organization; and leading a disciplined “friends of Egypt” grouping that promises substantial financial assistance but only to cushion the impact of overdue cuts in bloated subsidies.

With the American brand sullied by the perception of deference to the Brotherhood, the next phase of U.S.-Egypt relations will be rocky. Our influence is diminished but still exists. Egypt is important. Let’s get it right this time.