Harlan Ellison, the legendary, legendarily irascible speculative fiction writer who died this week at age 84, wrote the greatest episode of Star Trek ever made. And he hated it.

“The City on the Edge of Forever” aired on April 6, 1967, late in the original series’ first season, and won acclaim for capturing everything Star Trek could do at its best while suggesting weighty themes and emotional depths only hinted at in previous episodes. It won the Hugo Award for Best Dramatic Presentation and the Writers Guild of America Award for Best Episodic Drama on Television. Ellison accepted both. Neither salved his bitterness that the episode had been rewritten.

At the Hugos he dedicated the award to “the memory of the script they butchered, and in respect to those parts of it that had the vitality to shine through the evisceration.” “The City on the Edge of Forever” that aired may have been praised by virtually everyone who saw it, but it wasn’t his “City on the Edge of Forever,” and a compromised triumph was no triumph at all for Ellison. Ellison would spend the next several decades being publicly aggrieved by “City on the Edge of Forever.”

Was the reaction overkill? Of course. Overkill was part of Ellison’s persona. He held grudges. He deployed lawsuits liberally, sometimes successfully. (He’s now acknowledged in the credits of The Terminator thanks to one such suit.) He boasted of assaulting his publisher in the ’80s. And many never looked at him the same way after he groped author Connie Willis at the Hugos in 2006, for which he apologized — then grew angry when the apology wasn’t immediately accepted.

Ellison was famous for his contributions to science fiction and American literature, which extend well beyond his Star Trek script. But he was also famous for his grievances. The story of “The City on the Edge of Forever” represents that duality in miniature, and helps explain what made him both a beloved and divisive figure.

Star Trek’s best episode is credited to Harlan Ellison alone. It was a lie he would not let stand.

Here’s the version of “The City on the Edge of Forever” that’s been seen by countless viewers since 1967: After administering a small dose of a dangerous drug to Lt. Sulu (George Takei), Dr. McCoy (DeForest Kelley) accidentally administers a massive dose to his own abdomen after getting knocked about when the Enterprise hits some interference from a strange time distortion.

Driven temporarily mad, McCoy beams down to the nearest planet, home to the Guardian of Forever, a talking portal that allows visitors to travel through time and space. When McCoy uses it to travel back to Depression-era New York, the Enterprise’s landing party learns their ship has disappeared. Whatever McCoy has done has distorted history in such a way that the universe as they know it has ceased to exist.

Captain Kirk (William Shatner) and Mr. Spock (Leonard Nimoy) give chase, in time learning that McCoy has changed time by saving the life of Edith Keeler (Joan Collins), the near-saintly proprietor of a soup kitchen. If allowed to live, her idealistic message of pacifism and tolerance will delay the United States’ entry into World War II, allowing Hitler to develop the atomic bomb, win the war, and dominate the Earth — shutting the door on the hopeful future imagined throughout the series.

And so, as Spock says twice in the episode — first as a question then as a statement arrived at through cold, hard logic — Edith Keeler must die. The only problem: Kirk has fallen in love with her and isn’t sure he can bring himself to let her die. But, after reuniting with McCoy, he does just that, stopping the doctor from saving Edith from a truck that strikes her down in the street.

Many elements contribute to the episode’s greatness. The Guardian’s planet is an eerie, dreamlike place, one that inspires Kirk to comment, with understated poetic flair, “These ruins stretch to the horizon.” Journeyman director Joseph Pevney wisely lets the atmosphere, both of the alien world and 1930s New York, do a lot of the work.

Then there’s Shatner, who, often justifiably, gets a lot of flak for laying it on thick, but his performance here is measured. His love for Edith feels real, far removed from the flings seen in previous episodes. So does his heartbreak.

Yet much of the brilliance can be traced back to the script. Star Trek had raised philosophical issues before, but few as thorny as whether taking one life can be justified in the name of a greater good. And not just any life: Kirk falls for Edith because she’s virtuous and beautiful and finds him charming, sure, but also because she’s the living embodiment of the utopian principles he’s sworn to uphold as a member of Starfleet.

She believes in humanity’s potential to overcome hatred and selfishness, in the possibility of the better future in which Kirk lives. But to make that future possible, he has to let her die. She has the right message at the wrong time. It’s a Kobayashi Maru scenario in the form of a tragic romance.

It’s a near-perfect episode of television, recognized as such from the moment it aired. The credits bore only one name: Harlan Ellison.

Ellison knew it was a lie. He’d seen the script through several drafts, only to have it reworked, at Star Trek creator Gene Roddenberry’s insistence, by D.C. Fontana, Gene Coon, Steven W. Carabatsos, and Roddenberry himself. Ellison asked his name be taken off, but backed down. It would be the last time he backed down on this matter.

Most writers would sit back, take the praise, and keep quiet about the sausage-making process. Ellison wasn’t most writers, telling anyone who’d listen what had happened to his script, all the alterations and adjustments that made it lesser than the version he’d dreamed up. In 1975, during a short-lived rapprochement with Roddenberry, Ellison published the original version in his collection Six Science Fiction Plays, allowing the curious to compare and contrast the version they knew with the version that might have been.

Ellison’s version shares much of the filmed version’s bone structure. The time travel, Edith Keeler, the central moral question are all there. But it also contains a murderous drug-dealing crew member (an element Roddenberry found out of sync with his vision of an idealized future and a squeaky clean Starfleet), alternate-universe space pirates summoned into existence by the altering of time, 9-foot aliens (who would become the much more budget-friendly talking portal), and a World War I veteran named Trooper.

Most significantly, at the climactic moment, Kirk can’t bring himself to let Edith die. It’s Spock who makes the choice. Ellison saw Kirk as a man who, at a critical juncture, couldn’t let the love of his life die to save the universe. Roddenberry thought otherwise. The question of which feels truer to Kirk, and to Trek, serves as a litmus test for fans of the show.

Without Ellison’s talent and imagination, “The City on the Edge of Forever” wouldn’t have existed. Applying the butterfly effect to its absence — appropriate, given the episode’s plot — the Star Trek we know today wouldn’t have been possible without the ripples of complexity and moral ambiguity Ellison helped introduce to the series. (Not that Ellison had anything nice to say about the later series.)

But Ellison, whose early history includes multiple stories of running away from home, could seemingly never live comfortably in any world, even a world he helped create, be it Star Trek or the larger world of speculative fiction, which he helped shape with his work and his championing of other writers. Because Ellison could always imagine a better world, one in which “The City on the Edge of Forever” aired without evisceration, one in which the same sort of piggish shortsightedness that led to that evisceration wasn’t allowed to run rampant in so many aspects of life, one in which everyone finally saw he was right.

Reflecting on “The City on the Edge of Forever” years later, Ellison wrote, “The solitary creator, dreaming his or her dream, unaided, seems to me to be the only artist we can trust.” Ellison did a lot of that sort of dreaming. Sometimes the dreams went astray.

Ellison always had to have the last word. And then he’d just keep talking.

Ellison’s adventures in the TV trade — there would be more, and more frustrations — prompted him to write about television for the Los Angeles Free Press, unsparing observations collected in the influential 1970 book The Glass Teat and its sequel, The Other Glass Teat. It also assured he’d keep prose as his primary profession, helping to shepherd and elevate the literary careers of others.

The landmark collection Dangerous Visions, a collection of stories from science fiction stars and stars-to-be, appeared the same year as “The City on the Edge of Forever.” Again, Dangerous Visions followed in 1972. (A long-promised third volume never arrived.) He mentored Octavia Butler and others. He wrote. And wrote. And wrote. In a 2013 interview with the Guardian, Ellison put his tally at around 1,800 short stories, novellas, essays, and scripts. Today, “The City on the Edge of Forever,” both the filmed teleplay and Ellison’s original drafts, represent only a tiny fraction of his output and influence.

But even with his version of “The City on the Edge of Forever” available for the world to read, the matter felt unsettled for Ellison. It didn’t help that Roddenberry was out there telling his version of the story, claiming that Ellison’s script was filled with budget-breaking elements and that he had Commander Scotty dealing drugs.

Ellison knew better. The pirates were added at Roddenberry’s insistence and Scotty never dealt drugs in any drafts. He didn’t even appear in any drafts. Then there was all that money others were making from the episode, money that seemed never to find its way to Ellison.

This would not stand. So in 1995, four years after Roddenberry’s death, Ellison published “The City on the Edge of Forever” again, this time as a standalone book titled The City on the Edge of Forever: The Original Teleplay. The book includes two treatments for the episode; Ellison’s final draft of the screenplay; testimonials from Fontana, Kelley, Nimoy, and others; and a new introduction from Ellison designed to set the record straight.

The opening sets the tone:

“Speak no ill of the dead? Oh, really? Then let’s forget about a true introductory essay to this book. Let’s give a pass to setting the record straight. Let’s just shrug and say, ah, what the hell, it’s been more than thirty years and the bullshit has been slathered on with a trowel for so damned long, and so many greedy little pig-snouts have made so much money off those lies, and so many inimical forces continue to dip their pig-snouts in that Star Trek trough of bullshit that no one wants to hear your miserable bleats of “unfair! unfair” … that it ain’t worth the price of admission, Ellison.”

And so it goes for 90 profane, repetitive, discursive, hilarious, pitiless, insightful pages. It’s, in its own way, classic Ellison, who turned interviews into monologues. Smart interviewers generally knew to get out of his way and just let him talk. In the end, Ellison always had the last word. And then he just kept talking.

Ellison was sometimes too much, and too much in ways that are hard to excuse; offenses committed out of an excess of passion are still offenses. But, oh, that passion. Ellison simply had to fight back against every perceived slight and loss. He even had to fight back against any wins that weren’t on his own terms. He left behind miles of scorched earth and a towering body of work. He reshaped science fiction and changed the way his readers looked at the world. It wasn’t enough. Nothing ever was.