In its first season, HBO's comedy Silicon Valley sometimes felt a little formless. It was a tech satire that necessarily lagged behind the world it was parodying; not even the fastest TV production schedules can keep up with an industry that seems to move at light speed.

The show was frequently very funny, but more as an unintentional sketch comedy — with individual scenes that stood out within larger episodes — than as a traditional sitcom. Yet the season one finale featured the show's characters, perpetual underdogs, finally managing a big win, and that win provided just enough momentum heading into season two for Silicon Valley to turn the corner and become one of TV's most reliably funny comedies — and most skillfully plotted shows.

Here are just a few of the ways Silicon Valley became as deftly written as any serialized drama in season two.

It's so, so good at burying the foreshadowing

The season finale — which airs Sunday, June 14 — is positively packed with scenes that play off of moments from earlier in the season. But in almost every case, viewers won't realize that those earlier moments were laying track for what was to come.

For instance, consider the livestream of a condor egg that the team set up to show off the streaming capabilities of their Pied Piper app a few weeks ago. When they first turned it on, it was seen as a crushing disappointment, as they couldn't close a deal for a far more exciting, death-defying stunt's livestream, which was poached by a competitor. In the finale, however, the stream of the condor egg proves to be an integral part of the characters' greatest victory.

Too many TV shows draw so much attention to signaling what's ahead that the audience can predict almost everything that's going to happen. In Silicon Valley's best episodes, it feels almost like an exercise in sleight of hand; plot developments masquerade as punchlines to other jokes, so the audience is tempted to overlook them. That condor egg was so thoroughly established as a complete disappointment that I had almost even forgotten it existed — until it came up again.

It's great at balancing setbacks with payoffs

Truth be told, if season two had an issue, it was that, at 10 episodes long, the constant setbacks its characters endured could sometimes get a bit wearying. Every time Pied Piper appeared to be on the way up, somebody — usually the company's CEO, Richard Hendricks (Thomas Middleditch, who turns every line into a master class in comedic stammering) — would botch things, and everything would fall apart until the next episode.

At times it felt a little like Lucy pulling the football away from Charlie Brown. That's the way that Mike Judge, who co-created Silicon Valley and has written and directed several episodes, often likes to tell stories; he tends to find characters at their funniest when they're undergoing horrible suffering.

But sometimes you just want to see the Pied Piper gang succeed, dammit. And on that score, Silicon Valley's second season was better than its first. When the characters failed, it was always immediately clear just how they had screwed themselves over. And when they succeeded, it felt like they were triumphing over not just their opposition, but their own worst impulses. It made for a better mix.

It features Erlich Bachman — TV's best deconstruction of awful bros

The most frequently cited reference point in many reviews for Silicon Valley has been Entourage, HBO's series about awful bros being awful in Hollywood. And Silicon Valley undeniably has its share of cretins (though Richard tends to be a genuinely good guy). But what's important is that Judge and his fellow writers understand exactly how awful some of these people really are — and where that awfulness comes from.

Take Erlich Bachman, played by T.J. Miller (who should win all the awards for his performance). In season one, Erlich was clearly a satire of this sort of figure — clueless, self-impressed, endlessly convinced of his own sexual prowess. But he didn't have enough foils, so his noxiousness could be hard to take.

In season two, however, Erlich became a sort of "the devil you know" figure, especially when compared to tech billionaire Russ (Chris Diamantopoulos), a man who reduces everybody he meets to a nickname and is using a computerized voice to raise his child. Russ is the man whose approval Erlich seeks — and a warning sign of who Erlich could become — but he doesn't even notice Erlich, only increasing the latter's neediness. It's a great comic trope, and Silicon Valley executes it perfectly.

It's figured out how to make better use of its female characters

Silicon Valley is never going to boast a surplus of great roles for women. That's unfortunate, since there's so much to satirize about the tech world's resistance to women entering its sphere. (Just look at Reddit's revolts against Ellen Pao.) But the show is, ultimately, about how certain codes of masculinity assert themselves any time men get together — even if those men are stereotypical tech nerds. That doesn't leave much room for women.

However, whereas season one tended to make women the objects of the story, season two put them in positions of authority and power. This shift subtly revealed the way they worked to circumnavigate a world run by men who sometimes seemed completely clueless — even in how to simply talk to members of the other gender.

Everybody from Suzanne Cryer as the head of a company that owns stakes in Pied Piper to Romy Rosemont as the president of an online porn company turned in sly performances around the edges of this season, which only served to underline how incompetent Silicon Valley's main characters can be. And that only helped increase the conflict that drove so many of the season's best moments.

It's committed to digging deeper into all of its characters

Silicon Valley ultimately forces all of its characters to make choices — between right and wrong, easy money and hard work, good guy and awful bro. Season one occasionally coasted too readily on a hangout vibe, and there were stretches where very little actually happened. You couldn't say the same of season two, which constantly pushed the characters to new places to see what they would do.

Whether it was Dinesh (Kumail Nanjiani) constantly abasing himself to keep everybody in his life happy or Gilfoyle (Martin Starr) reveling in his seeming amorality, the season backed the characters into corners to see what they were made out of.

It's easy to forget that the heart of all storytelling — even comedic storytelling — is greater and greater conflict. But both Silicon Valley and its Sunday-night time-slot neighbor Veep (which also had a great season) have only gotten better by amping up the opposition their characters face. Here's hoping some of television's other, lazier comedies learn that lesson as well.

Silicon Valley ends its second season Sunday, June 14, at 10 pm Eastern on HBO. All previous episodes are available on HBO Go and HBO Now.