Tesla was back in the black during the third quarter of 2019. The electric car maker on Wednesday posted a surprise profit of $342 million. Wall Street had expected the company would post a net loss as big as $257 million. Tesla\n \n (TSLA) shares soared by up to 20% in after-hours trading. It marked the first quarter Tesla was profitable since the company posted back-to-back profits in the second half of 2018. Before that, Tesla had posted profits only twice since it went public in 2010 — once in 2013 and again in 2016. Much like the third quarter of 2018, last quarter’s earnings came when analysts were expecting another batch of losses. Also like last year, Tesla CEO Elon Musk had said months before the profit posted that the company would be out of the red, but analysts were skeptical. Tesla struggled last year to ramp up production of the Model 3 sedan, the company’s first car designed more for the mass market with price points starting around $40,000. Manufacturing issues at the company’s California factory appeared to be ironed out by the end of 2018. This year, Tesla has worked to expand sales abroad and has spent big bucks on a manufacturing plant in China in the hopes of accessing its billions of consumers. “We have also dramatically improved the pace of execution and capital efficiency of new production lines,” Tesla said in a letter to shareholders Wednesday. “Gigafactory Shanghai was built in 10 months and is ready for production, while it was [about] 65% less expensive to build than our Model 3 production system in the US.” Musk said during a call with analysts that the Shanghai factory would “become a template for future growth.” The plant is expected to also produce the Model Y, a cheaper version of Tesla’s Model X SUV that Musk said Wednesday could arrive to market sooner than expected — potentially as soon as mid-2020. If that holds true, “this will no doubt be a positive step for the company next year,” Jeremy Acevedo, senior manager of insights at Edmunds, said. He also noted that Tesla’s sales in the US have “sustained steady demand” even as American buyers have received smaller tax credits for their purchases. “But,” Acevedo added, “questions remain on whether the Model Y is a product that American shoppers can actually rally around in the same way that they’ve done with the Model 3 without cannibalizing other vehicles in the Tesla lineup.” Tesla reported that it brought in $6.3 billion in revenue last quarter, about on par with what analysts predicted. The company had already revealed the total number of cars the company delivered last quarter, which came in at about 97,000. That fell short of the 100,000 mark that CEO Elon Musk had hoped to hit — but it was still a record for the company, outpacing the 95,200 and 63,000 cars it delivered in the two previous quarters. Deliveries are, for the most part, the best indicator of how much money Tesla is bringing in because customers typically pay for their vehicles whenever they receive their cars. Tesla said Wednesday it is entering the fourth quarter with an order backlog and is focusing on “increasing production to meet that demand.” But the company faces headwinds shared by other automakers. Auto sales have been slowing around the globe for a variety of reasons, such as slowing economic growth and escalating trade tensions. US sales are down about 2% in the first half of this year, according to sales tracker Autodata.