Oscar and Grammy winner Paul Williams has cowritten classic songs for the Carpenters (“Rainy Days and Mondays”), Three Dog Night (“Out in the Country”), and Barbra Streisand (the Oscar-winning “Evergreen”). He cowrote the lyrics to The Love Boat’s unforgettable theme song (“Love, exciting and new!”). But his legacy song is one about spiritual search, higher callings, and the power of faith. It was written for a frog.

Not just any frog, of course—show business’s most beloved amphibian, Kermit the Frog. “The Rainbow Connection” opens The Muppet Movie, which celebrates its 40th anniversary this week (and returns to theaters July 25 and July 30). It’s a tune imbued with the spirit of Muppets creator Jim Henson: “What [cowriter Kenny Ascher and I] tapped into for Kermit is what we tapped into for Jim Henson; his mind, heart, and gentle soul,” Williams told Vanity Fair. “I think it’s his song as much as Kenny’s, or mine, or Kermit’s.”

The Muppet Movie marked the Muppets’ first foray into feature films, after decades of show-stopping appearances in TV commercials and variety shows; on Sesame Street; and as the stars of their own Emmy-winning variety show, The Muppet Show, which ran from 1976 to 1981. Muppet characters were also among the original Not Ready for Prime Time Players in Saturday Night Live’s first season—famously prompting staff writer Michael O’Donoghue to declare, disdainfully, “I won’t write for felt.”

Williams felt no such bias. He was a guest on The Muppet Show in 1976, and immediately felt welcome in the Muppet universe. Sesame Street, he said, was his “sanity pill” while on the road with his band. “I’ve been a fan of the Muppets before I knew they were the Muppets,” he said. “I always wondered what it would be like to work at Mad Magazine or the National Lampoon, or be a part of a writing team like Jerry Belson and…Garry Marshall and some of his crew. What fun it would be to work with the Muppets. It was a feeling like I was home. “

Henson first recruited Williams to write songs for Emmet Otter’s Jug-Band Christmas, a cult favorite that initially aired in Canada in 1977 and then on HBO a year later. Henson later asked him to write the songs for The Muppet Movie as well, giving Williams and Ascher untethered creative freedom. “One of my treasured memories is walking to the car with Jim after the [initial] meeting,” Williams recalled. “I said, ‘Jim, as we’re working on the songs, I’ll make sure you hear them to make sure we’re headed in the right direction.’ He said, ‘Paul, that’s not necessary, I’ll hear them in the studio.’ I went, ‘Wow…what trust.’”

The Muppet Movie tells Kermit’s swamp-to-stardom saga, an origin story introduced by “The Rainbow Connection.” Swamp-dweller Kermit plucks a banjo, contemplating rainbows and “what’s on the other side”—much like Judy Garland’s Dorothy in The Wizard of Oz. But “Somewhere Over the Rainbow” was not the song’s main inspiration.

“The model we used was ‘When You Wish Upon a Star,’ which opened Disney’s Pinocchio,” Williams said. “This is Kermit’s ‘I am’ song. This song will show that Kermit has an inner life, a spiritual life.”

With their very first verse, Williams and Ascher initially feared they had written themselves into a corner by debunking the concept of rainbows as magical: