This story was originally published by Slate and is reproduced here as part of the Climate Desk collaboration.

We all know the hallmarks of the classic business-themed Christmas movie. A good, well-meaning executive faces some tough business fundamentals as the holiday season approaches. Unexpected events deepen the gloom. But just in time for Christmas, a miracle arrives from on high, ensuring peace, prosperity, and happiness for years to come.

Help Grist raise $25,000 by September 30 to further advance our climate reporting

It’s a Wonderful Life? Yes. But it’s also the story of solar companies in the past few months. In November, things were looking bleak for the renewable energy sector at large and for solar companies in particular. The sector has been booming since 2009. The price of installing solar panels has come down sharply as scale has risen, new business models have hastened the spread of the technology, and giant companies are entering contracts to buy huge volumes of renewable energy. But none of that would have been possible without 1) the special federal tax breaks and credits for owners of solar panels, and 2) abundant capital seeking returns in a world of extraordinarily low interest rates. And in the second half of 2015, the investment thesis that kept solar stocks burning bright began to dim.

The solar investment tax credit — in which owners of solar-panel systems get a 30 percent tax credit — was always meant to be temporary and is set to expire next year. The Republicans in Congress generally favor fossil fuels over renewables, generally oppose anything President Obama is for, and deny the need to deal with climate change. So as fall settled in, investors began to focus on the fact that by the end of 2016, the solar investment tax credit of 30 percent would fall to 10 percent for commercial systems and disappear entirely for home-based systems.

Grist thanks its sponsors. Become one.

Another problem: Renewable energy is as much about financial engineering as it is about electrical engineering. For solar to work, investors had to believe that the structures rigged up to build solar would stand up over time. In recent years, energy companies had hived off renewable energy projects into special, publicly traded vehicles — yieldcos — that were supposed to pay dividends. But many of them failed to deliver expected results. Worse, the attractiveness of such fixed-income investments stands in inverse relation to the interest rates available elsewhere. And with the Federal Reserve telegraphing an interest rate increase in December, investors began to flee yieldcos.

Finally, the entire renewable industry depends to a large degree on the zeitgeist. When the world is in a green mood, when it looks like there will be a widespread, coordinated effort to combat climate change, investors get psyched about solar. When it appears that the will for collective action is fading, investors get the blahs. And throughout October and November, it was common to hear observers argue that the much-bruited Paris conference was going to be a bust, that it would deadlock over conflicts between rich and poor nations.

There’s a cruelty and ruthlessness to the markets, which can provide a fire hose of capital on Monday only to shut it off entirely on Tuesday. And that’s what began to happen in November. Stock markets are famously futures markets, and forward-looking investors suddenly didn’t like what they were seeing in the future. The stock of SunEdison, the self-proclaimed “largest global renewable energy development company,” fell from a high of $31.50 in July to a low of $2.86 on Nov. 19 — a loss of 91 percent. The stock helped sandbag the performance of well-known hedge fund manager David Einhorn, whose sale of a big chunk of SunEdison stock helped increase the melancholy. Analysts began to question the company’s liquidity, which is poison for a company with lots of debt. SolarCity, the giant rooftop-panel installer founded by Elon Musk and his cousins, saw its stock fall from $62 in May to $25 in early November, a decline of about 59 percent.

Like George Bailey, investors and executives at solar companies were essentially teetering on the bridge outside of town.

Grist thanks its sponsors. Become one.

And then a series of miracles happened. On Dec. 12, the Paris climate talks concluded with an unexpectedly strong agreement among countries to attempt to limit emissions. The U.S. publicly recommitted to green policies, and a large number of giant, influential global companies signed on to an initiative to get 100 percent of their energy from renewable sources. Investors began to reconsider their pessimism.

Next, Washington delivered — defying the conventional wisdom. Newly installed House Speaker Paul Ryan realized that he’d have to negotiate with congressional Democrats if he wanted to get a budget and tax deal before the end of the year. And as they came to the table, another miracle happened: The Democrats held fast. On Dec. 14, Democrats indicated they would be willing to support the Republican-backed effort to lift the ban on oil exports — but only if the Republicans would consent to measures including a multiyear extension of renewable energy credits. It worked. Last Friday, Congress voted to extend the 30 percent solar investment tax credit through 2019, and then to reduce it to 10 percent through 2022.

That move instantly made the U.S. solar industry viable for another six years. Investors were elated. SolarCity’s stock popped as details of the budget agreement began to emerge and then soared on its announcement. By Friday, the stock was above $56, up about 117 percent from its November low. SunEdison’s stock closed on Friday at $6.51, up 127 percent in a month. The Guggenheim Solar ETF is up about 30 percent from Nov. 19 through last Friday.

God bless us, everyone.