When Crazy Ex-Girlfriend first sang and danced its filthy, exuberant heart onto TV screens in 2015, the romantic future of Rebecca Bunch seemed clear: Obviously she was meant to be with Greg.

Sure, Rebecca might have been fixated on Josh Chan, the guy she dated in drama camp 11 years prior. But she was only projecting her own fixations and fantasies onto him, and she wasn’t interested in the person who Josh actually was. It was with Josh’s best friend, sardonic and self-loathing Greg, that Rebecca was able to be herself — and it seemed clear that in classic romantic comedy form, Rebecca would eventually realize that Greg was the guy she was meant to be with, and that he had been right there in front of her all along.

Maybe they wouldn’t actually end up together — maybe the ending would be dark and Rebecca would be too unhealthy to be with Greg, or maybe it would be uplifting and she would find that she didn’t need to end up with any man. But if Rebecca was going to end up with anyone and have it be a happy ending, then clearly the only reasonable option for her was Greg.

Then two things happened. First, Rebecca and Greg actually got together, and their relationship imploded spectacularly because both of them were way too unstable to be in a serious relationship with anyone.

And second, actor Santino Fontana left the show to return to his theater career. Which meant that Greg was no longer an option for Rebecca because Greg basically no longer existed. Until now.

In Friday’s episode, Crazy Ex-Girlfriend brought Greg back. The show brought him back to a new world, one in which Greg is no longer Rebecca’s clear romantic future, and they brought him back with a new face. Now, Greg is played by Skylar Astin.

Improbably, against all odds, it kind of works.

Here’s how Crazy Ex-Girlfriend managed to pull off recasting one of its biggest love interests, two years after he left the show.

1) The recasting works thematically

When Greg first appears onscreen, Rebecca doesn’t recognize him. “That’s not Greg,” she says impatiently. “That’s a completely different person.”

“Except for the part where it’s Greg,” the other characters tell her.

Rebecca’s confusion is more than a meta wink at the audience, and so is the title of the episode, “I’m Not the Person I Used to Be.” The hour is an exploration of how we change over time, and how our perceptions of people change.

In the two years that Greg spent offscreen, he did more than switch actors. He went into treatment for his alcoholism; he is no longer as self-loathing as he was before. He’s not the same person he was when he begged Rebecca to settle for him way back in season one — and Rebecca isn’t the same person she was back then, either. She’s in recovery now, too. She’s actively fighting her demons.

“You think I’ve changed,” Greg tells her at the end of the episode. “Maybe we both have.”

That thematic work means that Greg’s recasting doesn’t just feel like the show is bowing to the external pressures of making television and simply making the best of a bad situation. The recasting is a coherent metaphor that folds into the larger story that Crazy Ex-Girlfriend is telling, and it makes emotional sense.

2) Crazy Ex-Girlfriend is already a meta show

Greg’s recasting wouldn’t work as a metaphor if Crazy Ex-Girlfriend weren’t already given to frequently breaking the fourth wall. Not all of the show’s meta jokes land — personally, I found the runner in “I’m Not the Person I Used to Be” where characters would say that they’re in town “for the rest of the series … of holidays” to be way too cutesy — but they’ve become part of the vocabulary of Crazy Ex-Girlfriend.

This is a show that is heavily filtered through Rebecca’s perspective, and it consistently nods to that fact: That’s how it pulls off its musical numbers, and its theme songs, and its movie trailer parodies. All of which means that it was perfectly positioned to make lemonade out of the lemons of having to recast Greg.

Even so, it still wouldn’t have worked if it weren’t for two more key variables.

3) The show cast a good actor

Pitch Perfect’s Skylar Astin is a reasonable physical match for Fontana, which helps ease the transition here. But what’s more impressive is how carefully he matches the mannerisms that Fontana created for Greg — the slight stammer, the cheerfully sarcastic grin — without ever seeming to do an impression. It doesn’t feel like he’s doing Santino Fontana doing Greg; it feels like he’s just Greg now.

He has enough chemistry with Rachel Bloom (they went to college together!) to create a sense of continuity with the fraught Rebecca/Greg love story that was so central to the show in season one. Greg might no longer be positioned as the best bet for Rebecca’s endgame, but Astin manages to be compelling enough that I wouldn’t be mad if Greg and Rebecca somehow did end up together after all.

4) The recasting was done sincerely

What’s perhaps most impressive about Greg’s recasting is that it was played with complete sincerity. Rebecca’s confusion over her ex’s new face wasn’t a joke, it was genuine. And Greg himself is written completely straightforwardly, with no visible tweaking of the character to tailor the role to its new actor.

As written by Rene Gube in “I’m Not the Person I Used to Be,” Greg is pretty much the same guy, but healthier: He still has the sardonic charm that made him so likeable back in season one (“That’s funny, you are Greg, you’re funny,” Rebecca marvels when he cracks a joke), only now it’s less viciously self-loathing.

On a show that loves its meta jokes as much as Crazy Ex-Girlfriend, the temptation to wink at the camera about the recasting could have been overwhelming. But it could also have been fatal to the integrity of the character: Any hint that New Greg isn’t the same person as Old Greg to everyone except for Rebecca could have irrevocably damaged the emotional reality of the part. It’s only by playing it completely straight that Crazy Ex-Girlfriend was able to walk this tightrope.

Just before Greg’s reappearance, Heather and Valencia assure Rebecca that she and Greg are meant to be together. At one point in Crazy Ex-Girlfriend’s history, that felt true. It doesn’t quite anymore, and it’s easy to imagine a scenario where the recasting of Greg was the death knell for that particular love story.

But Greg’s reintroduction was so smooth that it feels like it’s opened new doors for the show instead. We’re not ever going to get back to the version of Crazy Ex-Girlfriend where Greg was Rebecca’s clear endgame, but there are a thousand other possibilities available for that storyline now. Only a show as madcap and exciting as Crazy Ex-Girlfriend could be trusted to pull them all off.