Electric-carmaker Tesla reported a quarterly profit on Wednesday, sending its shares higher and providing a measure of at least temporary vindication to embattled CEO Elon Musk.

Musk had said that Tesla would generate positive cash flow in the second half of this year, though some investors had expressed doubts, pointing to a tumultuous few months in which the billionaire’s use of Twitter led to legal trouble.

Tesla reported profit, or net income, of $312 million in the three months that ended Sept. 30, compared with a loss of $619 million in the same quarter a year earlier. Analysts had expected a small loss for the most recent quarter, Bloomberg reported.

Shares rose more than 12 percent in after-hours trading.

The results may have an impact on fears among some Tesla investors that the company would need to seek an infusion of cash in the near future by selling either bonds or otherwise in order to continue operations. Musk has said his company won’t need to raise money.

Tesla reported free cash flow of $881 million and an increase in cash or cash equivalents at its disposal. It was only the third time that number has been positive in Tesla’s history, Reuters reported.

Tesla generated $6.82 billion in revenue. Analysts on average had expected $6.33 billion, according to research firm Refinitiv.

Doubts about Musk’s leadership of Tesla grew after he posted a series of tweets in which he said he was considering taking Tesla private and had “funding secured.” His settlement of a lawsuit over the matter with the Securities and Exchange Commission requires that he step down as Tesla’s chairman, though he will remain as CEO.

Separately, a British diver has sued Musk for defamation over tweets Musk wrote.

Tesla in a letter to shareholders called it a “truly historic quarter” and reiterated that it expected to be profitable in the fourth quarter of the year, as well.

On Tuesday, a day before Tesla reported earnings, one of the investors who had been betting against the company reversed his position. Andrew Left of Citron Research said Tesla was “destroying the competition,” despite distractions like Musk smoking marijuana during the taping of a podcast.

But it wasn’t all good for Tesla. Consumer Reports said on Wednesday that the reliability of Tesla models had slipped from a year earlier, ranking the brand No. 27 out of 29 brands on its list of reliable vehicles. Based on surveys of its owners, the magazine found the Tesla Model X to be the second-worst vehicle on the road, and a growing list of problems with the Model S pushed the magazine to withdraw its coveted “Recommended Buy” rating. While the reliability of the newer Model 3 was rated “average,” the nonprofit’s testing chief Jake Fisher said that as more variants are launched, its reliability may also drop.

Tesla, though, continues to take deposits for more cars. It reported $906 million in consumer deposits for the quarter as it works through a backlog in demand.

The company says it made 4,300 Model 3s per week during the third quarter, excluding times when its factory had a planned shutdown.