On the evening of July 15, Major General Ibrahim Aydin was at a bridal party for a friend’s daughter at a hotel in central Ankara when warplanes began roaring low over the capital. The noise was one of the first signs that a faction of the military was attempting to seize power from the elected government. Aydin left the party, dropped his wife at home and headed to a military housing complex. By the next morning Aydin would find himself fighting his way through a Turkish military base in a battle for Turkey’s political future.

On the base he became one of an initial group of six generals organizing part of the response to the coup among the military units still loyal to the government. He called his commanding officer, Galip Mendi, the commander of Turkey’s gendarmerie, a militarized internal security force. He couldn’t reach Mendi or his second in command. He would later learn that Mendi had been taken hostage by the planners of the coup, and so Aydin and the other generals gathered dozens of members of the gendarmerie special forces to do battle with the coup faction which by then had commandeered tanks, helicopter gunships, and F-16 fighter jets.

They received no orders from President Recep Tayyip Erdogan or other political or military leaders. In two interviews, Aydin said the response from the loyal portions of the military was both organic and improvised. They sent one group of special forces to fight their way into the main gendarmerie base in Ankara at Bestepe, engaging in a fight that lasted two to three hours. Another group stayed behind, contacting gendarmerie commanders around the country to ascertain who had joined the coup and who remained loyal to the government.

“As a soldier you can’t always take your orders from your commander. Sometimes the situation commands you to act in certain ways, and you have to take initiative,” he says. “This is the equivalent of having your commander die in combat.”

As he and the other generals acted, civilian protesters also faced off with tanks in the streets of Ankara and Istanbul. More than 240 people died. But the loyal units of the military struck the decisive blow. If the coup had any hope of succeeding, its planners would have needed to persuade a majority of the armed forces to join them, and they failed. Instead, the military splintered, with key commanders actively resisting the insurrection.

While the coup faction bombed the parliament building and the President fled soldiers raiding the seaside hotel where he was staying, the loyal generals engaged in a high stakes effort to stop the insurrection. For Aydin and the other gendarmerie commanders, that meant suppressing the coup within their own branch of the armed forces. While he and other generals remained loyal, other units of the gendarmerie joined the coup.

Around midnight, Aydin realized that one of the nerve centers of the coup in Ankara was at an adjacent military base. Helicopter gunships from an adjoining army airbase at Guvercinlik were supporting the coup. Aydin and the loyalists waited to act, focusing instead on retaking their own bases. “The way we took over our headquarters, we expected the army to take over their base,” says Aydin.

Hours went by, and the helicopters kept taking off, so Aydin and his comrades decided to act, assembling a team of about 40 men, most of them on foot following two armored jeeps they brought for cover. The coup faction had blocked the entrance to the neighboring airbase with a fire truck, barricades, and a token guard. They took the sentries hostage and ordered them to roll the truck to the side. The sun was rising.

Their plan was to take over the base headquarters and the air traffic control tower. From the tower, they planned blow up the fuel tanks and debilitate the coup forces’ air power. Once on the base, they started taking incoming fire from the headquarters and from helicopters: eight sorties in all from three different helicopters. In frustration, a colonel in Aydin’s group fired his pistol at the gunships buzzing above them. They eventually seized control of both the headquarters and the tower, taking 14 coup participants prisoner. Four members of the loyalist raiding party were wounded. In the end they called off the destruction of the fuel tanks

Similar scenes unfolded in Istanbul, where the gunfire still echoed after dawn. In a pivotal moment, Umit Dundar, the commander of the first field army based in Istanbul, declared his opposition to the coup in a televised address.

Aydin had put in his retirement papers before the coup, and left the military about two weeks after the failed putsch. He claims that his and his comrades’ reaction to the coup was an automatic act of patriotism. “Putting aside the treason of these coup plotters, we judged this attempt as an embarrassment, an insult to both our army and to ourselves. For us, as part of that army, for anyone to be going this far in this kind of madness is entirely embarrassing. It’s an insult,” he says.

Aydin’s important role defending Erdogan’s government against the coup is extraordinary given that less than a year a ago, he was under arrest in connection with a high-profile national security investigation. The case dates back to 2014, when the gendarmerie searched a convoy of Turkish intelligence trucks headed toward northern Syria. Turkish news reports said the trucks contained weapons intended for Islamist groups in Syria, meanwhile Aydin (who was then the commander of the Ankara gendarmerie) and two other commanders were detained over the search. After a month in detention, he was released without charge in Dec. 2015.

The July 15 coup attempt rearranged Turkey’s political chessboard, and placed Erdogan in an unspoken coalition with officers like Aydin who faced controversy in the past. The government blames the coup on the followers of the powerful religious leader Fethullah Gulen, a former ally of Erdogan’s who lives in exile in the U.S. Turkish officials and analysts say Gulen’s followers have infiltrated the military and other state institutions for decades, building a network that they activated on the night of the coup. Although some foreign officials doubt parts of the government’s narrative of the coup, the blame placed on the Gulenists is universally accepted in Ankara. The narrative provides a point of unity for Erdogan’s Islamist-tinted government and the remaining military officers who are steeped in secular nationalist traditions.

But the unity masks a deeper sense of unease in Ankara. Now, as Erdogan moves aggressively to reign in the military, Erdogan finds himself relying on generals past foes. Nationalist generals previously alienated by Erdogan’s Islamist-inspired administration are now crucial to the government’s plans to reform the military. Even as the government sends tanks and warplanes into northern Syria to fight Islamic State and Kurdish militants, both the government and the military are still recovering from the shock of the coup.

“I think that this is a marriage of convenience, but this is a short term arrangement. Right now the Kemalist military and the government need each other,” says Gonul Tol, director for the Center for Turkish Studies at the Middle East Institute in Washington. “From the government’s perspective, Erdogan really needs a strong military under his full control to be able to project power in the region and also to continue the fight against PKK and the Islamic State. From the military’s perspective they really have to rebuild themselves. I think this is the biggest trauma they’ve suffered in their long history.”

After surviving the coup attempt, Erdogan and his government launched a historic attempt to reshape the armed forces. The authorities detained nearly 10,000 soldiers, including 143 general and admirals, or more than a third of the top military command. After declaring a state of emergency, the government issued decrees to bring the branches of the military under direct civilian control, with most branches now reporting to the defense ministry (the gendarmerie is under the interior ministry). The reforms appear intended to put the military out of the business of coups following decades in which the armed forces ousted four governments since 1960. Those arrested include elite pilots and numerous commanders serving in NATO-affiliated positions, illustrating the devastating effect of the coup on Turkey’s relations with western powers.

In a dizzying reversal of political fates, some of the officers that Erdogan can now trust are those who have been on trial in the past, accused of plotting coups. In a series of cases since 2013, hundreds of military officers were accused and convicted of plotting to overthrow the government. After Erdogan split with Gulen, those trials collapsed, the cases dismissed as Gulenist plots, sham trials based in part on forged evidence. In fact, five officers who were sentenced to prison in one of the previous coup trials, the so-called Sledgehammer trial, actively fought to stop the coup in Ankara in July according to Turkish news reports.

“Now all the people around Erdogan are potentially Gulenists. There is no way, no litmus test to realize who’s a Gulenist or who’s not. The only people Erdogan could trust are people who have been publicly victimized, vilified by Gulen, and who haven’t been in these circles for the last decade,” says Selim Sazak, a Turkish politics expert with the New York-based Century Foundation.

Turkey is reorganizing its armed forces at a moment of deep distrust among key political, judicial, and military leaders. The coup attempt and subsequent crackdown devastated the military’s morale and threw its ranks into disarray. In the aftermath, the government cut power to at least one base—Incirlik, which is used by U.S. force. Trucks blocked the entrances to others. In the wake of the bloody coup attempt, no one has voiced opposition to the planned reforms, but some former military officials are uneasy with the state of civil-military relations.

Retired general Ismail Hakki Pekin, a former defendant in one of the now-discredited coup cases, said of the government, “They know the importance of the Turkish military and they’re trying to increase the military’s morale. But at the same time there are also some negative things. Some people are still threatening the army, and they don’t like the Turkish army’s reactions.”

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.