When the Securities and Exchange Commission charged Tesla CEO Elon Musk with securities fraud, its main goal was probably not to shore up Tesla’s leadership structure, save Musk from himself, and put both the company and its leader on firmer footing.

That, however, could be the long-term outcome of the settlement the SEC reached with Musk over the weekend. What was intended to be a punishment of a rogue executive whose rash tweets had misled investors might turn out to be the best thing to happen to the electric-car company in some time.

First, let’s rewind. Musk plunged Tesla into crisis on Aug. 7 when he tweeted that he was “considering taking Tesla private” at $420 a share and had “funding secured.” It soon became evident that the funding was not secured, and by Aug. 25, Musk had dropped the plan altogether. By that time, Musk and Tesla were facing multiple investor lawsuits and an SEC investigation.

Meanwhile, Musk’s personal life was in shambles, as revealed by a weepy Aug. 16 interview with the New York Times and a feud involving his then-girlfriend Grimes and rapper Azealia Banks. He was clearly overwhelmed and exhausted—at a time when he was supposed to be guiding Tesla through some of the tightest straits of its wild journey to mass production of its make-or-break mass-market vehicle, the Model 3. He was also, of course, launching rockets as CEO of SpaceX, digging holes as CEO of the Boring Co., and digging himself into holes as CEO of his own Twitter account, through which he had accused one of the Thai cave rescuers of pedophilia. (Musk is now being sued for that, too.)

If this were the kids’ cartoon version of the Elon Musk story, it would have been the point at which the cheesy narrator asked: Could this be the end for our hero!?

It nearly was—at least for his career at Tesla. And Musk would have had no one to blame but himself.

On Thursday, after Musk reportedly backed out of a proposed settlement, the SEC filed a complaint in federal court that sought to remove Musk from Tesla altogether. It also sought to ban him from serving as an officer or director of any public company.

At the last moment, Musk’s self-preservation instinct kicked in. He accepted a deal that would remove him from his position as Tesla’s board chairman but allow him to stay on as CEO. Musk and Tesla will each have to pay $20 million, the company will add two new independent directors to its board, and it will have to do a better job of overseeing Musk’s public communications—including his Tesla-related tweets.

That’s more than a slap on the wrist. As corporate settlements go, it’s actually rather serious. But it could prove to be the medicine Musk and Tesla needed.

For some time now, Tesla watchers have been calling for an “adult in the room” to help oversee the company’s operations and serve as a check on Musk’s power. Musk has that at SpaceX in the form of COO Gwynne Shotwell, who is the rocket company’s clear second in command and runs its day-to-day operations. But he has never had it at Tesla, where he ruled both the company and its boardroom, and doubled as an intense micromanager on the factory floor.

And for years, the company’s actions and public statements have borne the imprint of Musk’s whims, with no moderating force to countervail them. His tweets have been swinging markets since long before he hit send on the fateful words “funding secured.”

In short, for all his technical wizardry, business brilliance, and marketing genius, Musk appears to be a man incapable of restraining himself. And at Tesla, he had built a corporate structure that was equally incapable of restraining him. Everyone knew Musk ought to stop tweeting so much—except for Musk himself, and at Tesla, he was the only one whose opinion counted.

Now, Musk and Tesla appear to have blundered their way into a settlement that will force them to do what they couldn’t do on their own: bring in some independent voices with real power. It will still be Musk’s company, and Musk made it clear with his first tweet after the settlement that Musk is still gonna Musk. But soon, for the first time in many years, he’ll have to answer to someone other than Musk. That’s a good thing for him, a good thing for Tesla, and a good thing for those who believe in the company’s mission of making electric cars mainstream.