It sounds like the sinister plot of some straight-to-DVD movie. Since 2009, Volkswagen had been installing elaborate software in 482,000 "clean diesel" vehicles sold in the US, so that the cars' pollution controls only worked when being tested for emissions. The rest of the time, the vehicles could freely spew hazardous, smog-forming compounds.

Suffice to say, regulators were livid once they caught on. Last Friday, the Environmental Protection Agency announced that Volkswagen had very flagrantly violated the Clean Air Act. Not only did the EPA order the German firm to fix the affected vehicles — which include diesel TDI versions of the Golf, Jetta, Beetle, and Passat — but the agency could end up levying fines as high as $18 billion. The Department of Justice is also contemplating criminal charges.

The scandal has only widened from there. On Tuesday, Volkswagen admitted that some 11 million clean diesel cars sold worldwide contain software to fool regulators, with the vast majority of cars likely to be in Europe.

Volkswagen, in other words, is in deep shit. The CEO, Martin Winterkorn, announced his resignation on Wednesday. The company has halted US sales of its 2015 and 2016 clean diesel vehicles and now has to fix millions of existing cars. It has already set aside $7.3 billion to deal with the problem. Meanwhile, VW's stock price has been plummeting, with the company losing one-third of its market cap in the past week:

This scandal raises a few larger questions, though: Why did Volkswagen cheat in the first place? And why was it so easy for the company to evade regulators for years? To get a better handle on the story, we need to take a brief trip through the tangled history of clean diesel vehicles — the specific cars that VW was selling.

Clean diesel cars were supposed to offer great mileage and low pollution — a tricky task

One basic fact to understand here is that there are two main types of combustion engines widely available today: diesel and gasoline. And there are real trade-offs to each.

Diesel engines have long been popular in Europe, and one of their major advantages is fuel economy. Diesel fuel contains more energy per gallon than gasoline, and the diesel engines work more efficiently. Put it together, and the typical diesel car can travel up to 30 percent farther on a gallon of fuel than its gasoline counterpart.

But there's a catch. While diesel cars get better mileage and emit fewer carbon dioxide emissions, they also emit more nitrogen oxides (NOx), which help form smog, and particulate matter, which can damage lungs. Both types of pollution can have serious health effects.

Due to higher NOx emissions, diesel cars didn't catch on in the US for many years

Historically, Europe has dealt with this trade-off by imposing relatively looser emissions standards on diesel cars in the pursuit of better fuel economy. Roughly one-third the passenger cars in Europe now run on diesel, and it's one reason cities like Paris have a serious smog problem. In the United States, by contrast, we've imposed far stricter rules around smog and other conventional pollutants since the 1970s, which is why diesel cars haven't caught on widely here: Until recently, few could pass America's stringent NOx standards.

Since 2009, however, things have changed. The Obama administration has been ratcheting up fuel economy standards in the United States, which puts a higher premium on mileage. At the same time, diesel technology has been gradually getting cleaner through a combination of lower-sulfur fuel, advanced engines, and new emission-control technology. So automakers have shown a renewed interest in "clean diesel" cars that, in theory, don't suffer from that trade-off between performance and pollution.

These vehicles have proved increasingly popular in the United States, even if they still represent less than 1 percent of the market. Since 2009, Volkswagen has sold more than 482,000 clean diesel cars containing a four-cylinder turbocharged direct injection engine. This included versions of the Passat, Jetta, Golf, Beetle, and Audi's A3.

Except, as it turns out, VW was lying about its clean diesel cars.

Volkswagen couldn't balance performance with low pollution. So it cheated.

Since 2009, we now know, Volkswagen had been inserting intricate code into its vehicle software that tracked steering and pedal movements. When those movements suggested that the car was being tested for nitrogen oxide emissions in a lab, the car automatically turned its pollution controls on. The rest of the time, the pollution controls switched off.

Regulators didn't notice this ruse for years. The problem was only uncovered by an independent group, the International Council on Clean Transportation, which wanted to investigate why there was such a discrepancy between laboratory tests and real-road performance for several of VW's diesel cars in Europe. So they worked with researchers at West Virginia University, who stuck a probe up the exhaust pipe of VW's clean diesel cars and drove them from San Diego to Seattle.

What the researchers found was shocking. On the road, VW's Jetta was emitting 15 to 35 times as much nitrogen oxide as the allowable limit. The VW Passat was emitting five to 20 times as much. These cars were emitting much more pollution than they had in the labs.

In May 2014, both California's air pollution regulator and the EPA ordered Volkswagen to investigate and fix the problem, and the company claimed that it had done so. Once again, the cars performed well in testing, but real-world performance still didn't match up. At that point, EPA regulators really started grilling Volkswagen about the discrepancy, even threatening not to approve the company's 2016 line of clean diesel cars. VW finally cracked and admitted the existence of these defeat devices, which had been carefully hidden in the software code. Scandal ensued.

Volkswagen hasn't explained exactly why it cheated, but outside analysts have a good guess. The NOx emission controls likely degraded the cars' performance when they were switched on — the engines ran hotter, wore out more quickly, and got poorer mileage. Some experts have suggested that the emission controls may have affected the cars' torque and acceleration, making them less fun to drive. (Indeed, some individual car owners have been known to disable their cars' emission controls to boost performance, though this is against the law.)

In other words, Volkswagen wasn't able to produce diesel cars that had the ideal mix of performance, fuel economy, and low pollution. (Or, at least, it couldn't do this profitably.) So the company "solved" this trade-off by sacrificing cleanliness and loosening the pollution controls. And it accomplished this via software designed to deceive regulators. This was wildly illegal, and Volkswagen got caught.

The VW scandal exposes problems with current emission tests

Volkswagen isn't the first company to cheat on its emission tests. As Frank O'Donnell of Clean Air Watch pointed out to me, the EPA caught a number of truck manufacturers, including Caterpillar and Volvo, doing something similar back in 1998 — programming their diesel trucks to emit fewer pollutants in lab tests than they did on the road. (The trucks would slowly emit more and more NOx as they traveled longer distances at constant speed, something that labs couldn't catch.)

Part of the problem here is that regulators usually test these vehicles under laboratory conditions, placing them on giant treadmills and requiring them to do a series of maneuvers. Because this process is predictable, it's easier to game. Combined with the fact that automakers are developing ever-more-elaborate software that can control and fine-tune engines, there are ample opportunities for fraud.

European regulators will soon start requiring on-road emissions testing

In theory, governments can find ways to make cheating harder. Starting with model year 2017 vehicles, European regulators are going to start requiring automakers to test their passenger cars on the road in addition to laboratory tests. That sort of regime would've made it harder for Volkswagen to pull its little stunt. But it's also unlikely this is the last time we'll see an automaker come up with a fiendishly clever way to cheat.

Meanwhile, the VW scandal raises another issue surrounding car regulations, as Alex Davies explains at Wired. Modern-day cars feature complex computer systems and software. And right now, this software is protected under the 1998 Digital Millennium Copyright Act — it's illegal to fiddle with the software. The ostensible rationale is to make it harder for consumers to tamper with emissions controls. But these protections also make it harder for independent researchers to scrutinize that code and identify problems. Some experts have proposed DMCA exemptions to allow researchers to test and evaluate these engines, but so far automakers and the EPA have resisted this. Presumably, if those exemptions had existed, Volkswagen's deception might have been caught sooner.

Volkswagen is now facing serious blowback

The US scandal wasn't the end of the story. On Tuesday, Volkswagen announced that some 11 million clean diesel cars sold worldwide contained these "defeat devices" — every car that featured a Type EA 189 diesel engine. Most of these cars are in Europe. (The company says its newest European diesel engines aren't affected and comply with EU pollution rules.)

At this point, Volkswagen has been caught red-handed and has to face the consequences. The company straight-up lied about its cars and knowingly evaded pollution limits. (Getting a precise estimate of the health damages caused by the extra pollution that resulted could be difficult, since it would depend on where the cars were located, how much extra smog actually resulted, and so forth.)

In response, the company has pledged to stop selling 2015 and 2016 Volkswagen and Audi models equipped with TDI clean diesel engines in the United States and will likely end up recalling existing cars to fix the problem.* It's still unclear how many customers will actually want to fix the problem, however, since, again, any patch might degrade gas mileage and/or performance.

Meanwhile, Volkswagen could face criminal prosecution — not to mention billions of dollars in fines. The Clean Air Act allows a fine of $37,500 per noncompliant vehicle. If the Obama administration really wanted to lower the hammer, that could total some $18 billion. (To put that in context, the company made a total profit of about $12 billion last year.) Volkswagen is the world's biggest automaker by sales, but it's not quite as profitable as competitors like Toyota and has struggled to gain a foothold in the US market. This could be a huge deal for the company.

This episode also raises questions about the future of clean diesel vehicles. Clean diesel appears to be a genuinely promising technology — in theory, such vehicles could get both excellent mileage and lower emissions. But this whole scandal raises serious questions about how well automakers can actually achieve both goals in practice.

*For any concerned US car owners, the models expected to be recalled include: The 2009-2015 VW Beetle 2.0L TDI; 2009-2015 VW Golf 2.0L TDI; 2009-2015 VW Jetta 2.0L TDI; 2009-2015 Audi A3 2.0L TDI; and 2014-2015 VW Passat 2.0L TDI.

Further reading: