Elon Musk, CEO of Tesla Inc

Tesla Inc shares soared on Wednesday after the company surprised investors by posting a profitable third quarter, boosted by record deliveries, cost cuts and improved production schedules for its new electric vehicle model.

Shares rose nearly 21% to $307.12 after hours on the surprise news, crossing $300 for the first time since March 1. Tesla on Wednesday posted a cash balance increase to $5.3 billion. The carmaker reported a profit of $1.86 per share, shattering analyst expectations for a loss of 42 cents per share.

The third-quarter results are an important milestone for Tesla and redemption for Chief Executive Elon Musk who had to step down as chairman after a series of scandals and investor doubts about Tesla’s ability to withstand competition from larger, better capitalized global rivals.

Tesla’s gross margins, an important profit indicator for investors, surpassed expectations and Tesla said it was “highly confident” in exceeding the low end of its yearly global vehicle delivery goal.

The carmaker said it had cut costs 16% on a yearly basis, citing improvements in operating efficiency and a reduction in manufacturing and material costs.

Tesla also expects to generally be cash flow positive as it has grown to the point of being self-funding.

But revenue fell nearly 8% to $6.30 billion in the quarter ended Sept. 30. Analysts had expected revenue of $6.33 billion, according to IBES data from Refinitiv.

In May, Tesla raised a record $2.7 billion in capital when investors scooped up a mix of new stock and convertible notes to boost its cash-depleted balance sheet.

Tesla shares had fallen by 23% for the year as of end of trading Tuesday. With Wednesday’s after-hours surge, Tesla’s stock has reduced its loss year-to-date to 8%.

Investors in the past have shown impatience with the company’s serial failures to meet financial and production targets. Earlier this month, Tesla shares slumped after the company reported delivering 97,000 vehicles for the third quarter, just short of analysts’ forecasts and only 2% ahead of the previous quarter.

But Tesla on Wednesday exceeded promises by billionaire Musk, who in July said Tesla would break even in the third quarter and turn a profit by the end of 2019.

The company has said it plans to deliver 360,000 to 400,000 vehicles for all of 2019, and on Wednesday said it was “highly confident in exceeding 360,000 deliveries this year.”

Analysts have questioned how rapidly Tesla’s vehicle sales will grow as government subsidies for electric vehicle purchases dwindle in the United States, China and other markets. U.S. federal tax credits for Tesla vehicles were cut by half on July 1 and will disappear by the end of the year.

Tesla also said production of its new electric SUV Model Y and its Model 3 vehicle factory in Shanghai were ahead of schedule. Model Y production is expected to launch by the summer of 2020, while production of full vehicles on a trial basis was already underway in Shanghai, Tesla said.

“We have cleared initial milestones toward our manufacturing license and are working towards finalizing the license and meeting other governmental requirements before we begin ramping production and delivery of vehicles from Shanghai,” the company said in a statement.

Tesla has said it aims to produce at least 1,000 Model 3 cars a week at the new Chinese factory by the end of this year, but it is unclear when it will meet year-end production targets due to uncertainties around orders, labor and suppliers.

The electric car maker’s net income attributable to common shareholders was $143 million, or 78 cents per share, for the third quarter, compared with $311 million, or $1.75 per share, a year earlier.

Musk is trying to contain costs while spending on major new initiatives.