Tesla has surprised everyone by turning a small profit for the first time in years. Elon Musk’s electric car company announced it had earned $22 million in the third quarter of 2016 — at a time when investors were bracing for a loss.

The news is huge for Tesla, which had been struggling to keep Wall Street happy as it raises billions in new capital for a major expansion. Up until this point, Tesla has been primarily a niche producer of luxury cars. But Musk has vowed to transform the company into a mass-market carmaker, and that’s going to require a lot more investment.

Tesla’s plan going forward is incredibly risky. Musk wants to begin production of the Model 3, a mass-market car that costs $35,000, by the end of next year. That will require a difficult expansion of production capacity — and Tesla has a long history of missing production targets.

At the same time, Tesla is placing a huge bet on self-driving cars — putting thousands of dollars of self-driving hardware into each of its cars before self-driving software is quite ready.

This approach could prove to be a huge strategic advantage if it allows Tesla to bring real self-driving technology to market years before anyone else. But conversely, it could become an albatross around Tesla’s neck if the company finds it has bet on the wrong technology — or if it takes longer than expected to bring self-driving software to market.

Scaling up a car company is really difficult

Silicon Valley software companies can often “scale up” from thousands to millions of customers in a matter of weeks simply by renting some extra servers and writing some extra code. But for a car company, the process is a lot more difficult.

That’s partly because it just takes a lot of expensive equipment to manufacture a car. Musk is planning to spend billions over the next few years building new assembly lines for the Model 3. Tesla is also building a massive battery factory called the Gigafactory to supply the lithium Ion batteries that power Tesla vehicles.

One of Musk’s big challenges here will be quality control. Cars are complex products that are expected to operate flawlessly for tens of thousands of miles in a wide variety of situations. Even minor production problems can introduce serious reliability or safety concerns, leading to product recalls that will cost millions of dollars.

Consistently getting these kinds of details right requires a regimented corporate culture very different from the freewheeling culture of a typical Silicon Valley company.

And indeed, as Tesla has scaled up production over the past couple of years, its quality ratings have plummeted. The early, high-end Tesla vehicles got high marks from Consumer Reports, but the latest rankings had Tesla near the bottom in quality rankings.

And Tesla expects to boost production even more rapidly over the next two years as it starts shipping out Model 3s. Musk has set a goal of producing 500,000 cars in 2018, which would represent a more than fivefold increase over this year’s output. There’s a big danger that Tesla’s quality problems will get even worse as the company rushes to boost production.

Tesla is taking a big risk with its self-driving strategy

A second risk for Tesla comes from its approach to self-driving capabilities. Tesla recently announced that it would start shipping $8,000 worth of self-driving hardware — cameras, sensors, and computing hardware — in every car the company ships. Musk says this hardware will eventually enable the cars to be fully self-driving. But the necessary software isn’t ready yet, and it might not be ready for several years.

If all goes according to plan, Tesla will be able to gradually upgrade the software on these cars to give them more and more powerful self-driving capabilities. At some point a few years down the road, Tesla will be able to push out software that enables full self-driving capabilities that could allow owners to type in a destination and take a nap in the back seat.

That extra hardware will allow Tesla to collect massive amounts of data about the landscape around customers’ cars. That could be extremely valuable because self-driving technology benefits from having detailed maps of the roads where self-driving cars are located.

Still, we don’t know how long it will take for this software to be ready. Google has been working on this kind of software for more than six years and appears to believe that it has several more years of work to do. So if the problem turns out to be harder than expected, that $8,000 worth of hardware could sit underutilized for years, dragging Tesla’s profits downward.

The other risk is that it turns out to be the wrong hardware for full self-driving capabilities. Tesla is pre-installing cameras and ultrasonic sensors in its cars. But it has chosen not to install LIDAR, an expensive technology that uses lasers to measure the exact distance to surrounding objects.

Musk argues that Tesla’s technology allows it to achieve LIDAR-like accuracy using cheaper sensors. If he’s right, Tesla will save money by avoiding the cost of expensive LIDAR hardware. But if it turns out that those other sensors aren’t reliable enough for full self-driving capabilities, that could suddenly make the millions Tesla has spent on other sensors look like a waste of money.

Watch: When carmakers taunted horses