It went down to the wire, and a little beyond, but the Writers Guild of America and major studios came to terms on a new contract early Tuesday, defusing the threat of a strike crippling the film and television business.

The sides shook hands shortly after midnight, minutes after the previous contract expiration deadline passed. The WGA had vowed to strike Tuesday if a deal was not in place. After a long week of negotiating sessions, including a Sunday gathering and a marathon run on Monday that began at 11 a.m., the guild and Alliance of Motion Picture and Television Producers clearly found enough common ground at the eleventh hour to sidestep the disruptive force of a work stoppage.

“Your Negotiating Committee is pleased to report that we have reached a tentative agreement with the AMPTP that we can recommend for ratification,” the WGA told members in a memo early Tuesday.

“In it, we made gains in minimums across the board – as well as contribution increases to our Health Plan that should ensure its solvency for years to come. And we further expanded our protections in Options and Exclusivity.

We also made unprecedented gains on the issue of short seasons in television, winning a definition (which has never before existed in our MBA) of 2.4 weeks of work for each episodic fee. Any work beyond that span will now require additional payment for hundreds of writer-producers.”

The memo added that the guild “won a 15% increase in Pay TV residuals, roughly $15 million in increases in High-Budget SVOD residuals, and, for the first time ever, residuals for comedy-variety writers in Pay TV.” “And, also for the first time ever, job protection on Parental Leave,” it went on.

“Did we get everything we wanted? No. Everything we deserve? Certainly not. But because we had the near-unanimous backing of you and your fellow writers, we were able to achieve a deal that will net this Guild’s members $130 million more, over the life of the contract, than the pattern we were expected to accept,” it concluded.

The negotiations between the sides were rocky from the start on March 13. The WGA achieved significant changes in the compensation structure for short-order TV series, forcing the studios to recognize the financial strain for TV writers as the industry norm has shifted to shows with six-13 episodes per season rather than the broadcast norm of 22-24.

Over the course of the talks, the companies sweetened their offers on several contentious issues, including the writers’ health fund, and the compensation formula and short-order series issue that became known by the short-hand of “span.”

Hollywood’s fears of a strike were amped up last month when the WGA secured a whopping 96.3% “yes” vote by members authorizing their leaders to strike.

Both sides had been negotiating for the past week with a looming deadline of midnight PT Monday, when the writers’ current contract expires. A strike would have required nearly all of the WGA’s 13,000 members to stop work immediately — hitting late-night talk shows, daytime soap operas, and “Saturday Night Live,” and disrupting upcoming TV series and films.

Underlining the high stakes, the CEOs of the major studios had been keeping close tabs on the talks as the deadline approached. Both the WGA and the AMPTP had imposed a media blackout and were not able to comment.

Negotiations for the new contract began on March 13 and broke off twice. The WGA repeatedly pointed to its calculation that the six major entertainment conglomerates generated $51 billion in operating profits during 2016 — including that figure in an April 28 message to members, estimating that it would cost employers $156 million annually to increase payments to writers under its proposal to production companies.

If the WGA had walked out, it would have been the guild’s seventh strike since 1960. The most recent strike was an acrimonious 100-day work stoppage, fueled by the WGA’s demand for new media residuals and jurisdiction. That strike started Nov. 5, 2007, and ended Feb. 12, 2008.