Silicon Valley ended its fourth season on Sunday after a string of episodes that even some of the HBO comedy’s most devoted fans have found a bit too cynical. For three years, brilliant programmer and Pied Piper company founder Richard Hendricks (played by Thomas Middleditch) has been the show’s bumbling hero: a gawky genius with great ideas, a good heart, and a nagging tendency to shoot himself in the foot. But then season 4 began turning Richard into a heel, at times as callously mean as all the tech billionaires who’ve been stealing his inventions and crushing his dreams since episode one. Even when Richard pulled himself back from the brink of rank villainy in the finale, his change of heart wasn’t an attack of conscience, so much as a matter of him having no other choice but to admit that an evil plan had fizzled.

Silicon Valley viewers are used to this series satirizing an industry where true innovators are exploited, while money flows unabated to any charismatic schmoozer who knows the right buzzwords. The show leans heavy on the classic Caddyshack-esque slobs-vs.-snobs dynamic of any good underdog comedy. But when it turns out that the people we’ve been rooting for are as sour and corrupt as the villains, the point of the show can become a little muddled.

There’s another way to look at what Silicon Valley’s doing, though. What if the show has never been as pessimistic as it seems? Maybe the stories have never been about “heroes” and “villains.” It’s a despairingly dark comedy, illustrating how talented-but-ruthless people screw each other over and keep a potential technological utopia at bay. But maybe it’s also been a clear-eyed look at how humanity keeps progressing in spite of ourselves.

Mike Judge co-created Silicon Valley alongside longtime collaborators John Altschuler and Dave Krinsky, and the show often gets compared to Judge’s cult movies Office Space and Idiocracy, which offer grim assessments of modern life and our possible future. Or at least they seem grim on the surface. Dig a little deeper, and there’s a brightness and sideways optimism to both those films, which belies their often sick senses of humor.

Office Space took a while to find an audience when it came out in 1999, but it eventually became a favorite of Gen-Xers once they began settling into a corporate culture geared toward treating grown-up employees like kindergartners. Judge’s insight into the forced bonhomie of co-worker interactions and the phony “do me a favor” tone of middle-manager speak rang painfully true. The movie put a frame around the hellishness of what millions of Americans in bland, blocky suburban office parks experience every day, clarifying just how soul-deadening their lives had become.

But all this is just a backdrop for Office Space’s story about a bored cubicle-drone named Peter (Ron Livingston) who begins failing upward in his company once he decides not to participate in the “we’re all friends here” charade anymore. Like Richard in Silicon Valley, Peter embraces amorality and makes dumb mistakes, but it’s still possible to read his story as an argument for resistance. Even in a system inclined toward dehumanization, the human spirit survives.

Idiocracy has also seen its reputation improve since it flopped at the box office back in 2006. The prescience of Judge’s vision — imagining an America 500 years from now, after generations of junk-food-scarfing, reality television-obsessed citizens breed a culture guided only by base impulses — gained new resonance in 2016, when populist anti-intellectualism vaulted a crude B-list TV star to the nation’s highest office.

Yet even in Idiocracy’s nightmarish version of the future (or metaphorically, the present), some form of organized society still exists, with enough well-meaning people to recognize the value of a moderately intelligent man like the movie’s hero, Joe (Luke Wilson). Judge may well believe the world is going to keep getting worse and worse from century to century, but he also clings to the idea that one individual can make a positive difference.

Consider the finale of Silicon Valley’s latest season, where Richard and his bickering, selfish Pied Piper partners Dinesh (Kumail Nanjiani) and Gilfoyle (Martin Starr) get a last-second reprieve from a potentially devastating failure. They end up engineering both the catastrophe and the fix themselves, unwittingly. A scheme to sneak malware onto smartphones at a tech convention — to house a client’s data cheaply and prove the viability of Richard’s design for a decentralized internet — causes those devices to explode. The manufacturer, Hooli, then announces a recall that could become a costly setback to the Pied Piper team. It’s only thanks the sturdiness and flexibility of Richard’s program, coupled with Gilfoyle’s hobby of pranking Dinesh, that the data which otherwise might’ve dissipated into the cloud instead redistributes securely into a network of web-connected refrigerators that Gilfoyle hacked.

Silicon Valley does feature some truly awful people, and Richard is one of them. Season 4 has been especially pointed about how the Valley’s oversized population of nerds, bros, and opportunists has fostered a business culture where pettiness is rewarded, not scorned. The finale even takes a clear shot at Donald Trump by having dunderheaded Hooli boss Jack Barker blame all his problems on the previous administration, while over-promising big changes that he lacks the knowhow to oversee. In his Trumpiest moment, Jack insists he can make an impossible deal with the Chinese due to his phenomenal negotiating skills, which mainly consist of him saying “just work harder.”

But what’s always made this series more than just a harsh snapshot of a nasty place is its plotting, which emphasizes characters who act as much as they react. Silicon Valley shies away from the mild complications of most TV comedy situations, preferring full-blown crises that the characters have to slug their way through. In their ingenious solutions — even the ones that don’t work — the show finds its heart. Richard repeatedly lets his ego lead him into disaster, and in the scramble to escape, he sometimes betrays his principles and screws over his friends. But he never stops thinking, and he keeps finding potential ways to wriggle free every time he seems trapped.

In the latest season, even when Richard’s intended ends don’t justify his increasingly malevolent means, he’s not wrong when he says he’s working toward something genuinely important: a new kind of internet that will be faster, cheaper, and easier for anyone to access. For him, this is the latest iteration of what’s been a cycle of inadvertent discoveries. Richard launched Pied Piper as an advanced app for preventing musicians from accidentally aping copyrighted songs. His application required the development of a compression algorithm that’s since become the core of Pied Piper’s business, improving longstanding technological problems like file storage, choppy streaming video, and fuzzy video chat.

In other words, for all the boneheaded moves Richard and his Pied Piper mates make, they still continue to generate useful products. Even as they snipe at each other and behave abominably, over and over they end up working together to come up with something astonishingly clever. Richard, like Peter in Office Space and Jack in Idiocracy, is a messy mix of remarkable and lousy qualities. But all three of these characters forge ahead in their own way, and — like the rest of us — keep falling forward.