A record number of cars shipped in the third quarter of 2019 were enough to help Tesla turn a modest profit, according to financial figures released by the Silicon Valley automaker on Wednesday. And with the Shanghai Gigafactory nearly ready to begin production of the Model 3, Tesla’s most affordable car, all eyes are now on China to see what kind of lift the automaker can get in the biggest market for electric vehicles in the world.

Tesla reported a net income of $143 million, and generated $6.3 billion of revenue, which is down slightly from the second quarter, and down about $520 million from the third quarter of 2018. That marks the first year-over-year quarterly revenue decline for Tesla since 2012, though the company attributes the drop to a tripling in the number of customers leasing its cars. (Tesla started leasing Model 3s in April of this year.) The company said it ended the quarter with $5.3 billion in cash.

A record quarter for deliveries helped Tesla get back out of the red

Tesla says it turned its first profit since the fourth quarter of 2018 by slashing operating costs, which are the “lowest level since Model 3 production started” — likely aided by multiple workforce cuts the company has made, and Elon Musk’s “nano” managing. The company also benefited from selling $134 million in regulatory credits, and booked $30 million of revenue from the money it has charged customers over the years for the “full self-driving” version of Autopilot. Tesla plans to count more of that “deferred revenue” as profit in coming quarters as it rolls out new features that are part of that “full self-driving” software package. (This quarter, it shipped the “Smart Summon” parking feature.)

On October 2nd, Tesla announced it delivered around 97,000 cars in the third quarter of 2019, meaning the company just edged out the 95,356 it shipped in the second quarter. Despite getting off to a slow start in 2019 (due in part to the company shifting focus to starting deliveries of the Model 3 in Europe and China), Tesla has already shipped more cars in three quarters this year than it has in all of 2018, thanks in large part to the popularity of the Model 3 — especially because Model S and X sales have dropped nearly 40 percent compared to the third quarter of 2018.

But that slow start means Tesla has to pull off an even more impressive fourth quarter just to hit the low end of what Elon Musk estimates for the year, which was that the company would ship between 360,000 and 400,000 cars. Still, Tesla turned a profit for the first time since ending 2018 with back-to-back profitable quarters; Musk has made repeated predictions that the automaker would eventually lift itself out of the red.

This is where the new Gigafactory comes in. China is the biggest market for EVs and, despite a recent downturn in both the automotive market and overall economy, still represents a huge opportunity for Tesla. Until now, all of the cars Tesla sold in China were made in the US and shipped to China, making them subject to tariffs and the shifting winds of the trade war. Producing cars locally means Tesla should be able to sell more in China, even with a sagging Chinese economy.

Tesla says the Gigafactory in China is ahead of schedule

In its letter to investors, Tesla says its factory in China is “ahead of schedule,” and that the company is already producing Model 3s “on a trial basis.” Tesla says trial production of the Model Y, which is slated to begin in mid-2020, is ahead of schedule.

Tesla’s Shanghai Gigafactory didn’t come easy; the company spent years lobbying and negotiating to find a way in. It wasn’t until China announced plans in July 2018 to relax rules that previously required foreign automakers to partner with Chinese car companies that Tesla inked a deal to build its third Gigafactory.

After months of site selection and final negotiations, Tesla broke ground on the Shanghai Gigafactory in January of this year. The factory took shape at a rapid pace, with construction workers operating around the clock to raise the giant building out of what was a field of mud. Now, Tesla says, the Shanghai Gigafactory is “ready for production.” Tesla also says the new Gigafactory cost 65 percent less than the Model 3 production system it built out in the US, which is scattered between the Gigafactory in Nevada and the company’s original factory in Fremont, California.

Update October 24th, 8:20AM ET: Added information about Tesla’s deferred revenue for the quarter.