Why Did Reagan Coddle Iran (and Hezbollah)?

I've always been puzzled why Reagan didn't try to punish Iran for taking American hostages. And then barely reacted when Hezbollah killed over 200 marines, and tortured CIA station chief Buckley to death. And then tried to sell weapons to Iran. Near as I can tell, it's because the Reagan Administration had one huge priority, and that was to defeat Communism. (Although we did back Saddam for a while against Iran.) Fanatical muslims, Administration officials hoped, were potential allies of the U.S. against godless Communism, just as they turned out to be in Afghanistan. Is this a reasonable summary? Any commentors with expertise on this?

You can't really blame the Reaganites too much, as they did indeed defeat Communism. But they also seem to have sowed the seeds of WWIII, by allowing and sometimes encouraging fanatical anti-Western Sunni and Shiite Islamicism to flourish, just as Roosevelt and Truman laid the groundwork for the Cold War by cooperating with Stalin to defeat Hitler--and being way too naive about their putative "ally."