Volkswagen is ditching coal.

Europe's biggest automaker has been burning the dirty fuel to power its giant assembly plant in Wolfsburg, Germany. It said Thursday that would end.

Volkswagen (VLKAF) will invest €400 million ($494 million) to modernize two power stations in the factory complex, converting them from hard coal to natural gas.

The two refurbished plants, which also supply heat to the city of Wolfsburg, should be using the new source of fuel by 2021 and 2022, it said.

"The Volkswagen Group ... must make its contribution to combating climate change and improving air quality," CEO Matthias Mueller said in a statement.

The company is trying to position itself as an environmental leader by ramping up its production of electric cars. At same time, it is still dealing with the aftermath of its global diesel scandal. Volkswagen admitted in 2015 it had rigged engines to cheat on emissions tests.

Half of the company's value was wiped out when the scandal broke, and sales of its cars plunged. Volkswagen's business and share price have recovered strongly, but there could be more penalties and lawsuits are still pending.

The scandal is having political consequences, too. Germany's top court paved the way for major cities to ban diesel vehicles last month, and other cities and countries are looking to do the same.

Related: Volkswagen used to love diesel. Not anymore

Volkswagen has pledged to reduce its environmental impact by 45% by between 2010 and 2025. It is hoping the new power plants will make a significant contribution towards this goal.

"The new facilities ... will also drastically lower water consumption, waste volumes and other emissions," said Stephan Krinke, the group's head of environmental affairs.

Related: Monkeys were used to test diesel fumes

Germany relies on coal way more than other developed countries. According to the International Energy Agency, 43% of Germany's electricity is generated from coal, compared to 31% in the US, 9% in the UK and 2% in France.

The country is trying to transform its energy supply and has set itself ambitious targets to cut emissions, but it is falling behind on them.

Chancellor Angela Merkel's new government has promised to set a target date to stop burning coal in power stations.