You’re the Worst’s renewal yesterday was a happy surprise — a surprise because its ratings aren’t great, but its fans will be happy because it’s turned into one of the best shows on television.

The show is a great, dark romantic comedy. I like to describe it as being a show built on the bitterness of seeing happy wedding and baby pictures on your Facebook feed, and with that, it’s always been nastily funny. But most of its plaudits this season have gone to its depiction of depression, with Aya Cash’s character Gretchen suffering from debilitating mental illness as the season has gone on.

But something that’s been less commented-on is how fantastic this second season of You’re the Worst has been from a structural standpoint. When we talk about well-designed seasons of televisions, we usually talk serialized dramas. But here’s a sitcom incorporating those lessons, and using them to become both funnier and more emotionally affecting — with last night’s episode, “Other Things You Could Be Doing”, the second-last of the season, bringing all that together for a gloriously chaotic heartbreaking/heartwarming climax to an astonishing season.

So what makes for good structure? How did You’re the Worst incorporate so much good across a dozen episodes?

The first thing a good seasonal structure needs is an instantly recognizable story and/or theme. This often takes the form of a season-long villain, as popularized by Buffy and The Sopranos, with their “Big Bad.” Each season is directly associated with a villain: Season 3 of Buffy is The Mayor’s season, Season 2 of The Sopranos is the Richie season. Or there’s The Wire, which used each season to examine a different part of an American city, Season 2 for blue-collar unions, Season 4 for public schools.

You’re the Worst absolutely nails this. This is the season of Gretchen’s depression. What’s more, it’s arguably the best depiction of depression in popular visual media, looking at it as a complex force of near-inexplicable personal difficulty, instead of something with straightforward causes and solutions.

And both Aya Cash, portraying it, and Chris Geere (Jimmy) trying to help as her boyfriend, nail it from a performance perspective. She spends the better part of three episodes as little more than a miserable lump, doing next to nothing. But the potential for her to break out, to snap an angry or hilarious monologue, laugh at Jimmy’s pain, or burst into tears, makes her utterly magnetic at doing nothing.

Second, a structurally great television series needs a variety of storylines. This can mean a few different things. First, the show can’t have the same plot throughout. It needs multiple smaller stories alongside the big one, like the fourth season of The Wire covering the personal stories of its five teenagers alongside the wider stories of the mayoral election, school district plans, and the rise of drug lord Marlo Stanfield.

Allen Maldonado as Honey Nutz, Brandon Mychal Smith as Sam, Darrell Britt-Gibson as Shitstain in FXX's 'You're the Worst'

You’re the Worst has done a remarkable job of this, incorporating all of its major characters, and several of its minor characters, throughout the season. Gretchen’s client Sam and his rap group had a fake feud, but its resolution ends up helping her best friend Lindsey attempt to fix her life. That all collides in “Other Things You Could Be Doing” which gives the episode a huge sense of resolution.

That variety of storyline also works linearly — a show can’t just say what its storyline is, and stick with that for ten or 13 or 22 episodes. Perhaps the best example of this is arguably my favorite season of serialized television, Justified’s second season. That season introduces its villain (the superb Margo Martindale and its themes of the inescapability of history almost immediately. But every three episodes or so, the plot focus shifts: here the villains are just getting in the way, but the introduction of a coal mining company and a new criminal force upset the balance, leading to multiple climaxes across the season.

Keeping the story fresh is probably the toughest structural form to deal with — it’s often the problem with hyper serialized shows like Game of Thrones where the knowledge that the big events don’t really happen until the end of the season can make earlier episodes feel like a chore.

Which is part of what makes You’re the Worst’s second season so astonishingly good — it takes that model and makes it darkly comic. What starts as a season about Jimmy and Gretchen living with one another and struggling with trying to remain hip and not-boring becomes a mystery about why Gretchen is sneaking out at night. That’s revealed to be depression, and the show shifts into her trying to fight it, and pretend everything’s alright. In the last few episodes, she’s essentially given up, and her friends try desperately to help her — or get pushed away.

This sounds serious — and it is serious — but it’s also a huge help for the comedy because the show gets to use Cash’s performance as an anchor for different styles. When she’s fighting the depression, the show is acerbic, letting her take bitterly insulting bites into all the characters around her as they act normal. But when she’s fully depressed, she’s a blank slate, and it’s the other people acting different and desperate around her. Different jokes, some bleaker than others, but they all work.

The final part of a great seasonal structure is variety of episodes. It’s not enough to just have a strong story, but each individual chapter of that story needs to have the potential to be notable. The third season of Breaking Bad is another essential, classic piece of television, largely for its mid-season shift from rising dread to constant danger and terrifying decisions for survival. Yet in the middle of all that is “Fly”, a bottle episode that puts the show’s two main characters in a physically safe place, while mentally they drain one another, exposing secrets, achieving emotional catharsis. “Fly” is consistently cited by critics as being arguably the best episode of the series, although there are some (bizarre) fans who think it’s among the worst for interrupting the action. But regardless of feelings about the individual episode, it helps focus the episodes around it as being not-”Fly”, action-packed compared to its dialogue-heavy two-character play.

Bryan Cranston as Walter White in AMC's 'Breaking Bad'

You’re the Worst has succeeded remarkably here as well. While it’s consistently worked at creating tight individual sitcom episodes, twice this season it’s broken its format — and those are its two best episodes. In “There Is Not Currently a Problem,” the show dives into the always-fruitful bottle episode, trapping most of its characters in an apartment with increasingly absurd tension as Gretchen’s depression becomes impossible to hide.

And then there’s “LCD Soundsystem”, the only episode of the series that’s not actually funny, instead more of a 20-minute short film about the desperate need for happiness and normality. By taking the perspective away from Gretchen and Jimmy, and putting it onto a new couple, we saw the ostensible series protagonists as objects instead of subjects, showing them and their problems in an entirely new light, and making Gretchen’s turn from anger into resignation seem like a tragic and ironic process, instead of a sudden shift. Plus when the series returned to conventional comedy, it was all the funnier for having seen another perspective.

The “Golden Age of Television” of the past two decades is largely understood as being built on serialization experiments in prestigious hour-long dramas. You’re the Worst is in the process of unceremoniously shoving its particular brand of highly-structured comedy into those conversations. I can’t wait to see what it attempts in its third season.