Republicans are already dancing on the grave of Solyndra, the solar panel manufacturer that received a $535 million federal loan in 2009 and collapsed on Wednesday. Here’s more music they can dance to: Sources tell me the Obama administration restructured the loan this winter, so taxpayers probably won’t even be the first creditors to get paid after Solyndra files for bankruptcy next week. The first $75 million will go to two Solyndra investors who poured in extra cash when the company nearly went bust in January. And one of them is a venture associated with the billionaire George Kaiser, an Obama campaign bundler.

The other investor is a partnership associated with the Walton family, which tends to lean Republican. And public filings suggest that Kaiser-linked funds had sunk at least $320 million into Solyndra before adding the secured financing; they’re taking a bath along with the rest of us. “If this was a sweetheart deal, it was the worst sweetheart deal ever,” one official quipped.

So why did the administration agree to the restructuring? The short answer, in poker terms, is that it felt pot-committed. It had already made a big bet; it didn’t want to fold if there was still a chance of winning. The slightly longer answer is that administration officials thought (as I did) that Solyndra was back on track, and that giving the company a new lease on life would benefit taxpayers even if it ultimately failed. A fuller explanation culled from government documents follows.

Solyndra’s loan, the first approved under a clean-energy program that was launched during the Bush administration and expanded by Obama’s stimulus bill, was supposed to finance a new state-of-the-art factory for the company’s unique cylindrical solar cells. At the time, Solyndra was an exciting startup; according to the public filings, it attracted big money from bigtime financiers, including $35 million from Richard Branson’s Virgin Green Fund, $57 million from U.S. Venture Partners, and even $2 million from affiliates of Kohlberg Kravis Roberts.

Obama visited the factory in 2010, and hailed it as a beacon of innovation. But by that time, Solyndra was a mess; it soon cancelled an IPO and fired its management team. The biggest problem was obvious; in an industry where prices were plummeting, Solyndra’s product was too expensive. It desperately needed to finish its new factory, which would increase volume and decrease costs. And it needed more sales.

By last November, the company was running out of cash; according to a January 2011 government document, it had “a very high probability” of bankruptcy and liquidation. This was a big problem, not only because the company had drawn down $460 million of its loan, but because its new factory wasn’t even completed, which meant liquidation would be a fire sale. The administration estimated that Solyndra’s assets would fetch less than $100 million, for a total loss of over $360 million.

The other option was restructuring. Kaiser’s Argonaut Ventures and the Walton family’s Madrone Partners would put up an additional $75 million, which would take the first position in case of a liquidation; the government would still be paid first if the company managed to emerge from bankruptcy. Meanwhile, the Department of Energy brought in independent consultants to analyze the company’s technical, financial and market assumptions, and ultimately concluded it did have a potentially viable business. The new factory was on time and on budget. Sales were increasing steadily. And even if Solyndra failed, it would be much more valuable with a completed high-tech plant than with an empty box in Fremont, California.

“We were already in deep,” one official recalls. “We looked at every relevant scenario to maximize the recovery for the taxpayer. We did due diligence as if this were a brand new transaction…The takeaway is, at every juncture we did whatever we could to ensure the best possible outcome for the taxpayer. I think we structured a pretty impressive deal.”

In other words: The operation was successful, but the patient died. Politically, it’s probably an impossible case to make. But that doesn’t mean it’s wrong.

The government estimated that if the company still went bankrupt but emerged as a going concern, it would still be worth $240 million to $480 million, so the loss to taxpayers would be much lower. And the new management team-led by a former Intel executive-made a persuasive case that it could turn things around with a new sales strategy and a more efficient factory. And it did. Until it didn’t.

“The restructuring gave Solyndra a fighting chance for success,” that same official says. “But then everything fell off a cliff.”

In the summer of 2011, solar panel prices plummeted again. The investors had been poised to inject another $75 million, but this time, they decided not to throw good money after bad. Solyndra shut down and laid off its 1,100 employees. It certainly doesn’t look like it’s ever going to be a going concern again, although bankruptcies can surprise. Thanks to the restructuring, the company’s intellectual property is now part of its collateral.

Solyndra drew down $527 million of its loan before shutting its doors, so the government would have to get back about $160 million from the bankruptcy just to match what it thought it could have gotten if Solyndra had collapsed in January. And the first $75 million goes to Argonaut and Madrone. So unless the company’s assets are worth $235 million, the administration is going to have even more explaining to do.

This is sure to play out as a scandal, but based on what we know so far, it shouldn’t be. Private loans go south all the time. The federal loan guarantee program has budgeted $2.5 billion for failures like this; so far, the program has made about $30 billion worth of loans, and has leveraged another $20 billion in private financing.

The Obama administration has made bets on hundreds of clean-energy companies in dozens of clean-energy sectors; some of those bets in its portfolio are bound to go bad, just as Richard Branson picks an occasional lemon. It’s legitimate to question whether the government should have made this particular bet, or whether it overplayed a weak hand, or whether it should be making bets in the first place. But if we’re going to have a clean energy industry in this country, this kind of thing is going to happen. It doesn’t mean anyone cheated.