[Spoiler alert: This post contains plot details for “Silicon Valley” Season 3, Episode 10.]

Sunday night’s “Silicon Valley” season three finale found the Pied Piper guys back where they’ve been off and on since the series’ beginning — broke, living in Erlich Bachman’s house and working for a startup that may be totally worthless. It’s a happy ending.

Thanks to an eleventh hour save by Bachman [T.J. Miller] and Nelson “Big Head” Bighetti [Josh Brener], Pied Piper avoided being sold to and scrapped by evil tech giant Hooli. It was a surprising end to a strong, satisfying season for HBO’s highest-rated comedy series.

Executive producer and showrunner Alec Berg directed the finale — which premiered three weeks after HBO gave a series order to “Barry,” a new series from Berg and comedian Bill Hader. Berg spoke with Variety about the finale, next season and the storyline that had to be killed to save season three.

Did you know from the beginning that Bachman and Big Head would own the company at the end of the season?

We kind of didn’t know where we were headed at the beginning of the year. We spent all this time setting up Barker and having the guys toil under a hostile CEO. Early in the season, they conceive this skunkworks, this company inside the company, and as we started writing that, our intention was that that was going to be the bulk of the season. And it just wasn’t giving us the comedy that we wanted. It was just a lot of very farcical stuff — someone knocks on the door and everyone has to hide their plans. So we called an audible in the middle of the season. That skunkworks episode, they did all the planning and then at the end of the episode, it all blows up in their face. That honestly was just the most interesting way we thought of to get out of skunkworks.

It was a surprise when the skunkworks blew up at the end of that episode.

That was very much by design. It was our hope that the audience would be surprised by it, because in all honesty we were sort of surprised. We spent a week or two in the room trying to write skunkworks episodes and being like “Why is this so hard? It should be giving us loads of material, and it’s just binding us.”

When did the Bachman-Big Head solution come into focus?

Once we blew up skunkworks, very quickly we got to “then they fight with Jack and they beat him and now they’re back in the house on their own.” I kind of view the season in two halves, and that was half one. Then half two kind of came together in one big piece. So we wrote episode one, then two, then three, then four, then five. Then we laid out six, seven, eight, nine, 10 all at the same time.

It feels like an optimistic finale even though the company is essentially worthless and they have no money now.

But they have a product that is growing organically the way that nothing they’ve done before really has, and they control their own destiny. They’re not beholden to anybody else. You’re right that it’s optimistic, but Richard’s reputation is garbage right now and what they’re making is a product that other people make. And they have no money. So it leaves us with a lot of work to do as writers. Also we seed in that there’s going to be a lot of disagreement over who owns what. In theory, Erlich and Big Head own all of it. But none of those other guys are going to work at a company that they don’t have equity in. So there’s going to be a lot of negotiating.

Should we expect to see more Big Head next season?

Well, he owns 50% of the company now. Big Head has spent the last two years off in the Hooli wilderness, fighting his fights elsewhere. So it feels like it. To be honest, we start writing [for next season] on Monday, so we don’t have a real sense yet of where the next season goes.

Monica talks in the finale about maybe needing a job at Pied Piper. Could the show benefit from having a female character in the house?

We’ll see. We kind of intentionally left that vague. The tricky thing is I kind of do think having Suzanne [Cryer, who plays Raviga Capital head Laurie Bream] in the show is important. I think having a grounding VC presence is important. So if Monica leaves, does that mean that we start to say goodbye to Raviga? I don’t know.

How many seasons can the show run for?

I don’t know. The massive challenge of doing this show is I think it functions when these guys are outsiders. And I don’t know if these guys actually start to really win, if this show is as compelling. On “Entourage,” Vinny could get the big movie and get $10 million and they could go buy Ferraris, and that was voyeuristic and fun. But I think if these guys end up becoming the next Google, my fear is that the show is sort of over. So the real, existential question is how can we keep finding interesting ways to keep them away from the finish line? Or does it just become like Lucy and the football, and you just become annoyed and like, “If these guys can’t figure it out by now they’re just stupid and I’ve lost interest.”

“Barry” was just picked up to series. How are you going to balance time between the two?

Poorly, probably. We don’t start writing that until October, so I’ve got July, August and September full time on “Silicon Valley” to try to get us as far as I can. And we’ll just figure it out. We’ve got great people working on this show. Mike Judge is no slouch. It’s not as if the show doesn’t exist when I’m not in the room. So I defer to a lot of very talented people, and hopefully no one will notice.