The fourth season of Silicon Valley ended with a rare win for Pied Piper.

After an uphill battle that involved patent trolls, exploding phones and explicit smart fridges, Richard Hendricks (Thomas Middleditch) and the rest of the gang have finally cleared some major obstacles on their path to creating a new, decentralized version of the Internet.

"It's really fun to watch these guys scramble and fail, but it's also fun to watch them succeed," says Mike Judge, who serves as the showrunner on the HBO comedy alongside Alec Berg. "At some point, we just thought that we should figure out what they're going to pivot to, and that really big play would take them to the end of the series."

The episode also marked the end of the road for T.J. Miller's character, Erlich Bachman, following last month's news that the actor would not be returning to the show's fifth season. In the end, Erlich was left in an drug house in Tibet with enough money to keep him there for at least five years.

Ahead of the season finale, The Hollywood Reporter caught up with Judge to talk about Richard's deteriorating moral compass, why this has been Zach Woods' breakout season and his endgame for the series.

What made you want to end the season the way you did with the guys actually winning for once?

At the beginning of the season, we started thinking that maybe we shouldn't have them fail and pivot again to something else. On one hand, I think it's really fun to watch these guys scramble and fail — but it's also fun to watch them succeed. At some point, we just thought that we should figure out what they're going to pivot to, and that really big play would take them to the end of the series. That's the plan anyway.

How did the idea for Hooli-Con come together?

That's something that we've talked about doing in both season two and three. We're been talking about having one of those giant tech conferences like Dreamforce and CES of our own for a while, and we finally found a way to make it work with what they were doing so that it was organic to the story.

How difficult was building an entirely fake tech conference?

All the companies that you see in there except for the ones that are characters are real. We got people to volunteer to come and just set up their stuff in order to be in the show and be seen. I think they thought it would be fun — but one of the challenges was that these are people with some very successful tech companies and I think they're not used to be told by an AD [assistant director] to be quiet and to stand there all day long or to move their mouth but not make a noise. And ADs are used to very obedient extras. It was a little awkward. A lot of these companies are successful tech people and suddenly it's like, "No, don't stand there. Be quiet." [Laughs.] And they don't really care if they don't ever work as an extra again. They were all really nice, it was just kind of funny to watch. It's a very different dynamic than you typically have on a set in L.A.

We've seen Richard evolve over the course of the show's four seasons on the air. What made you want to test his moral limits?

There has been a Breaking Bad element to this season. I think what happens to him is what I've seen happen in Hollywood, actually. You see it with somebody like a head of network. For example, I know Judy McGrath got into what she did when she ran MTV all those years because she's a true music fan and loved what she was doing — but in order to stay in power, you have to start doing stuff that is not true to your inner music fan. If you just refuse to compromise, you get fired and somebody else takes over and it's even worse. I think that's where Richard is at. If he's too nice, he's going to lose control over this whole thing. In his mind, he can do more good than bad by shaking hands with the devil a little bit, and I think that's what makes it interesting for the rest of the series is how far is he willing to go down the dark path in order to ultimately make things better and build this Internet that he thinks could be good for mankind?

One character that has really shined this season is Jared, played by Zach Woods. Did you anticipate this season being the breakout it has been for him?

If you ask anyone on the show including the actors, "Who is your favorite?" Zach comes up a lot. I think most of the main cast would probably say that. It's funny, for the first couple seasons, Zach was improvising a lot of stuff about his dark past and his dark childhood and foster care. And I usually cut most of it — not because I didn't think it was right for the character or because I didn't think it was funny — but for other reasons it didn't fit. Then we started finding ways to make it really work where it was organic to the scene. So a lot of that really did come from Zach himself. He's a brilliant performer and just a brilliant comedian. It's been really fun to write for him. On the page, he's kind of the moral center, but seeing him doing it just took it to another level. It was better than I even thought I could be. I think this season finale might be my favorite episode so far. Zach is just so good.

How'd you come up with the idea to have Jack Barker (Stephen Tobolowsky) get kidnapped in a Chinese factory?

That's another thing that we'd be wanting to do for a while. That actually really happened to a CEO, though he was going there to lay off a bunch of people and they held him hostage. I studied that and shot it the same way where he was looking through those bars. The guy wasn't armed but there was something funny about it. In the writers room, we just kept watching it and there was something funny about it because it was a kind of passive-aggressive kidnapping. There weren't any guns; it was just like, "There's 5,000 of us and one of you, and you're not getting out of the building."

HBO confirmed last month that T.J. Miller wouldn't be returning for the show's fifth season. How did you decide to write him out the way you did?

It was kind of becoming clear that he didn't want to do the show anymore, but we wanted to leave it so that there would be an opportunity to come back at some point. And so we just talked about how to do that. We had already written the part about Gavin going off to a monastery.... So we thought, maybe we have Erlich go look for him. And then when the season was done, we talked to T.J. and said, "Do you want to come back for part of it?" And he just wanted to move on. We wanted to give him an out if he wanted to go.

So could he could back for even just one scene next season? Or is this the last we'll see Miller in the show?

That I don't know for sure, but that's probably the last [you'll see him]. Down the road, if there's a season six, I don't know. You never know. But that's the last for a while at least.

As a core castmember and series regular on the show, didn't Miller have a contract? Are you willing to allow an actor to break it if he really wants out?

That would be HBO's [area]. I don't know what the contracts are. But I think if somebody doesn't want to do it, you don't want to force them to. I certainly don't. That's what I said. I don't take any pleasure in making people do something they don't want to do. I think some bosses do, but I don't. It also wouldn't make for a very good work environment.

How do you plan to fill the void left by Erlich moving forward?

We've talked a little bit about it. We start writing the next season on Monday. A lot of times in the writing, it's almost a struggle because we have a lot of characters in the show and a lot of good ones. I'll certainly miss having him in the show. I don't think we're going to say, "Hey, here's a new character to replace him," or anything like that. New characters come along. The tech world is so rich with weird characters. I think we still have more characters we can still pull out of the bag. But I also think we can do a lot more with Suzanne Cryer's character and certainly Zach and Jian Yang. I'm actually looking forward. I think we can shake things up in a way.

You also recently added Haley Joel Osment, who plays a virtual reality wunderkind. Will he be back next year?

Oh right, that's another. We'd certainly like to have him back. We haven't figured out how yet because we haven't started writing, but we all really liked working with him. I wouldn't look at that as someone who replaces Erlich because there's a very different energy to him, but I really like that character and VR is a huge thing in the tech world so it makes sense with that, too.

Presumably, you've been following the recent Uber saga. Could that provide fodder for a season-five plot?

We had written most of last season when that started to go on so it didn't really make its way in there, but that certainly seems like something we can look into. There's other stories that we've heard, not about Uber, but just in general about douchebag, sexist VCs. There's at least one or two that I've heard that still haven't made their way into the show. I think it will more likely be in that area based on something that wasn't in the newspaper. But I'm sure we'll find a way.

You've said before that you're aiming for a six-season run, so are you viewing this next one as the penultimate season?

Season five would be the second-to-last, yeah. That's how we were talking when we were setting up this season finale, just to help us in the writing. But you never know. It could be that it gets to six and someone has some idea.

And do you know exactly how you want to end Silicon Valley?

Yeah, we have a series finale, a place ahead that we've talked about for a while now just amongst ourselves. That's how we're thinking of it.