After several years of underwhelming sales, has Tesla finally cracked the code of peddling electric cars to Chinese buyers?

This week the automaker revealed in a filing that revenue from China in 2016 tripled to more than $1 billion, up from just $318 million in 2015.

While Tesla’s U.S. sales dwarfed that figure, accounting for about $4.2 billion in revenue, China’s emergence as a growing opportunity for Tesla is significant. The country is the largest market in the world for electric cars, partly thanks to strong government support, but Tesla -- like many foreign car companies -- has struggled to sell to Chinese buyers.

Tesla started delivering its Model S sedans to Chinese consumers in early 2014. But a year later, CEO Elon Musk admitted that sales of Tesla cars in China had been “weak” due to buyers’ perception that the country lacked adequate charging infrastructure. In general, it’s also been notoriously difficult for foreign companies to tweak their products to the individual preferences of Chinese consumers.

In 2014, Tesla reported $477 million in sales from China. In 2015, that number dipped to $318 million due to delays and market troubles. 

In order to localize its cars better, Tesla later launched higher-end options like “executive rear seats” geared toward Chinese car owners that have drivers. Tesla has also built out Supercharger stations and destination chargers around the country to offer Chinese Tesla owners better charging options.

According to some estimates, Tesla delivered about 7,500 electric cars to Chinese buyers in 2016. At $120,000 a pop, that would bring in $900 million. Tesla sells its cars to Chinese consumers for between $104,000 to $121,000.

At one point, Tesla had a goal to ship 10,000 electric cars to Chinese consumers in 2016, but early last year the company lowered that annual target to 5,000. Ten thousand Tesla cars that sold for $120,000 each would bring in revenue of $1.2 billion, so it appears that Tesla at least beat its benchmark of shipping 5,000 cars to Chinese consumers.

Still, that’s not a ton of cars. Tesla delivered just under 80,000 cars to all markets in 2016. So its Chinese sales could account for just an eighth or even less of the total shipments. Norway -- a small country, but one that has aggressively supported electric cars -- generated $335 million in revenue for Tesla.

In contrast to Tesla’s Chinese car shipments, Chinese automaker BYD sold tens of thousands of each of its electric car models -- including the Tang, the Qin and the e6 -- last year, according to estimates. Other Chinese automakers that are moving electric vehicles include Chery, BAIC, Geely and Zotye Auto. Indeed, the Chinese market offers some of the most electric-vehicle options of any market in the world.

Many of BYD’s cars are much cheaper than Tesla’s. That’s partly because the Chinese car and battery maker has been producing lower-end models than Tesla’s, but also because BYD can take advantage of Chinese government subsidies that foreign automakers like Tesla can’t.

Warren Buffett is a major investor in BYD, putting the investor in competition with Musk. Buffett and Musk have also competed in other clean energy markets: Berkshire Hathaway's NV Energy charged into battle against SolarCity in Nevada.

Tesla could see even more of a boost in China when it eventually introduces its lower-cost Model 3 to Chinese consumers. That car is supposed to cost $35,000 for U.S. customers. The first Model 3s will be shipped to early reservation holders later this year.

Tesla doesn’t reveal details of the components that make up its revenue in China, but it’s likely mostly -- or even entirely -- electric car sales. The company has declined to provide more information on the sources of its Chinese revenue.

Tesla only started growing deliveries of its grid batteries, called Powerpacks, in late 2016. The company has installed grid batteries in California, the Hawaiian island of Kauai, Connecticut, North Carolina, New Zealand and the U.K.

However, grid batteries could someday be a substantial source of revenue for Tesla in China. The country is the world’s largest market for solar and wind, and batteries can complement clean energy by storing energy when the sun stops shining and the wind stops blowing.

Tesla’s Chinese revenue isn’t huge, but it shows how the U.S. automaker has turned the corner on the difficult market. At one point, Musk had hoped that sales of the Model S in China would be equal to its sales in the U.S., which is  a goal that clearly hasn’t been met yet.

Musk has also long talked about opening a manufacturing facility in China. Car companies must make cars in China to be able to avoid Chinese government tariffs and potentially qualify for Chinese government subsidies. Musk has said “there is an argument for having... [China] be our first major factory outside the U.S.”

But Tesla is already investing heavily in manufacturing at its new battery factory outside of Reno, Nevada, as well as at its facility in Fremont, Calif. Without raising more money, Tesla appears to be committing its current funding to manufacturing in the U.S.