WARNING: This article contains major spoilers for the Season 4 finale of Supergirl, "The Quest For Peace."

Lex Luthor had it all planned out. He put all the pieces in place, and, as he sang along to Frank Sinatra's "My Way," he donned his power suit and singlehandedly took on the Kaznian attack on the United States. A grateful nation finally saw Lex as the hero he believed he was. As the Season 4 finale of Supergirl began, Lex had everything exactly where he wanted. Ultimately, however, it was his obsession with bringing down Superman that led to his undoing.

Lex's endgame wasn't just to become the public's hero, ee also wanted to revolutionize energy worldwide — by using aliens as batteries. Before powering Earth, though, he planned to use the first batch of energy to power a weapon that would reduce to ash Argo, the planet where Superman and the rest of the surviving Kryptonians reside. Even Eve Teschmacher didn't like the idea, but Lex was insistent.

RELATED: Supergirl: Lex Luthor's Plan Comes to Fruition With Latest Death

On Shelley Island, where Lex's facility collected alien power, Dreamer, J'onn J'onnz and the other aliens band together to take back control. Dreamer and J'onn overload the system so it can't wipe out Argo. Realizing that something is wrong and Superman isn't about to be destroyed, Lex dons his power suit and flies to the facility, where Supergirl, Alex and James Olson are already fighting Agent Liberty and his cronies. Lex furious Kara still alive, because Red Daughter insisted she had killed her before, before she herself was overpowered by Lex and placed in a battery to drain her energy. Lex sees the same resilience in Supergirl that he does in Superman -- and in his book, that's not a good thing.

Lex Luther in the Supergirl Season 4 finale

In the ensuing fight, Supergirl uses the anti-Kryptonite shield Lena created. It's Red Daughter, however, who saves Supergirl. After J'onn and Dreamer overload the system, they release the imprisoned aliens, including Red Daughter. When she finds Lex and Supergirl fighting, she jumps in front of a blast from Lex. She dies from the effort, and Supergirl seems to become infused with her energy. From there she has the power to take down Lex.

RELATED: Batwoman: First Trailer Declares 'Batman Gave Up On Us'

Lex tries to fly away in his power suit, but it falls apart around him. Supergirl attempts to save him, but Lex refuses, and appears to crash into the ground, hundreds of feet below. Unknown to anyone but Lena, Lex actually survives by teleporting to his secret lair. Expecting him, Lena quickly injects Lex with the antidote for the Harun-El super-powering him, and then aims at him the gun she confiscated when overpowering Eve Teschmacher.

Lex doesn't believe Lena has to the fortitude to hurt him, but he's horribly mistaken. He barely finishes his thought before Lena shoots him twice, explaining, "The world will never be a safe place with you in it." Lex applauds her gumption even as he slowly dies. The bullets are fatal, though, and Lex, ultimately takes his last breath.

Could this be the start of an even more ruthless Lena? She's tried so hard to avoid going to the dark side like her brother, but this ending demonstrates a sharp contrast between her and Supergirl. While Supergirl tried to save Lex, even after everything he did, Lena was the one to take him out.

Airing next season on Sundays at 9 p.m. ET/PT on The CW, Supergirl stars Melissa Benoist as Kara Danvers, David Harewood as Martian Manhunter, Mehcad Brooks as Jimmy Olsen and Chyler Leigh as Alex Danvers.