The wrangling went down to the wire as midnight approached on May 1.

Leaders of the Writers Guild of America and the Alliance of Motion Picture and Television Producers huddled around the long rectangular table in the conference room, purpose-built for collective bargaining, at AMPTP headquarters in Sherman Oaks.

The industry let out a collective cheer — and sigh of relief — when word surfaced shortly after the witching hour that the sides had reached an agreement on a new master film and TV contract. For the first time in a decade, the WGA and the major studios and networks had come to the brink of a work stoppage. The high-flying peak TV era would have been quickly grounded if writers had set down their pencils — or keyboards — on May 2.

By many accounts, the WGA and the majors were uncertain of the outcome of the negotiations as late as 90 minutes before the contract expiration deadline. So what turned the tide? Insiders credit relationships forged across the table during past negotiations for helping ease tensions at key moments. In the end, the accord came through the hard work of compromise and both sides educating the other on the issues that mattered most.

Writers considered the need to address outmoded compensation formulas for scribes increasingly working on short-order TV series, and the need to shore up the guild’s health insurance plan as strike-worthy items.

The concerns on both were strong enough to allow the guild to achieve a 96% margin of approval from members in a strike authorization vote. The memory of the 100-day writers strike in 2007 and early 2008 hung heavy over the discussions and the public pronouncements as the WGA went into war footing.

Both camps engaged in a little saber rattling in March and April as negotiations stopped and started.

Behind the scenes, however, there was a growing level of understanding between the sides. The AMPTP member companies acknowledged there was a need to adjust the compensation template for writers who were effectively taking per-episode pay cuts by working for longer periods on shows with episode orders of anywhere from eight to 13 per season compared to the 22- to 24-episode norm enshrined for decades on broadcast TV. This marked a big change from the 2007-08 negotiations when the philosophical differences over compensation issues became insurmountable. This time around, the heavy lifting went into coming up with a new formula that both sides could live with.

“The way in which the television industry has migrated to shorter orders had made part of our contract anachronistic,” said David Young, executive director of the Writers Guild of America West. “Our members were taking a 25%-50% pay cut on their episodic fee. We had to deal with that in a way that worked for both parties.”

Insiders credit Christopher Keyser, pictured above, a veteran shorunner and former WGA West president, and WGA West assistant executive director Chuck Slocum as key drivers of the effort to wrestle the short-order math problem to the ground. Keyser, who was co-chair of the WGA negotiating committee, was intimately familiar with the strain that short-orders have put on writer income levels. He came up through the world of broadcast TV dramas, notably Fox’s “Party of Five,” but more recently has worked on cable and streaming series (FX’s “Tyrant,” Amazon’s “The Last Tycoon”).

Another breakthrough moment came when CBS chief Leslie Moonves and Warner Bros. boss Kevin Tsujihara held a stealth meeting in April with four members of the WGA negotiating committee. One of the participants was Meredith Stiehm, a showrunner who was well known to Moonves as the creator of the CBS drama “Cold Case” and more recently an exec producer of Showtime’s “Homeland.” Stiehm explained to the honchos in plain terms that she was making half as much working on 12 episodes of “Homeland” than she did a decade ago delivering 22 episodes of “Cold Case” because of the outmoded fee structure. The fact that Moonves and Tsujihara traveled to Sherman Oaks on a Sunday afternoon to hear out the WGA team also went a long way to establishing goodwill during a rough patch in the negotiations.

“By and large [the companies] were pretty upfront early on in the negotiation,” Young says. “They could see there was a problem.” (The AMPTP declined to comment for this report.)

Eventually, the WGA came up with a new per-episode formula for short-order shows that was put on the bargaining table in a form that allowed for back-and-forth with AMPTP negotiators. Once they had something to work with, one observer says, it simply became a question of the “power dynamic.” The writers pushed hard and the studios gave up just enough to avoid seeing picket signs on May 2.

The urgent problem of the WGA’s deficit in its health insurance problem also was solved by both sides moving by degrees toward compromise. WGA leaders had to do some internal diplomacy to bring all of its negotiating team and board members around to the harsh reality that the guild would have to make some painful cuts in benefits — to the tune of $7 million over three years — in order to secure a $30 million influx from the AMPTP.

For the WGA, getting enough money to shore up the plan for the long haul was paramount. Otherwise, the health plan’s fiscal problems were sure to remain an urgent issue in contract negotiations every three years. And that would mean the WGA would give up leverage to push hard for gains in bottom-line issues of compensation, residuals and creative rights.

With the benefit of hindsight, it’s clear that both sides came to the 2017 negotiations ready to make a deal.

The strike threat was very real, and so was the pushback from the AMPTP companies early on. But the foundation of relationships — notably between AMPTP president Carol Lombardini and WGA West executive director David Young — was much stronger than in 2007. There was also a collective will to avoid the economic pain of a strike. Leadership and compromise saved the day, delivering a groundbreaking contract that kept writers’ keyboards humming and the cameras rolling.