The Beatles’ classic ballad “Yesterday” has a dreamy and gentle melody, with lyrics that feel plucked out of a man’s deepest subconscious. So it’s only fitting that rock legend Paul McCartney came up with the earliest stages of the song in his sleep, then let them pour out of his soul on the piano next to his bed. Even better, that initial inspiration happened in the attic of the apartment where he lived with his then-girlfriend, who shaped so much of his music throughout the Beatles years.

“I woke up with a lovely tune in my head,” he told author Barry Miles for the biography Many Years From Now, which was published in 1998. “I thought, ‘That’s great, I wonder what that is?’ There was an upright piano next to me, to the right of the bed by the window. I got out of bed, sat at the piano, found G, found F sharp minor 7th – and that leads you through then to B to E minor, and finally back to E. It all leads forward logically. I liked the melody a lot but because I’d dreamed it I couldn’t believe I’d written it.”

It’s quite the magical and romantic story, and a credit to just how masterful a song “Yesterday” is that it stays magical and romantic even once you know that McCartney’s dream ditty eventually became a breakup tune and that its now-immortal lyrics were once gibberish placeholder rhymes about scrambled eggs. The ballad hit the top of the U.S. Billboard Hot 100 chart in September 1965, capping a songwriting process unlike any other in the Beatles’ storied run.

READ MORE: How the Beatles Got Together and Became the Best-Selling Band of All Time

McCartney's ex Jane Asher came into his life a year before he wrote "Yesterday"

The “Yesterday” dream revelation came to McCartney one morning in 1964, but the story really begins in 1963, when he met Jane Asher backstage before a concert at Royal Albert Hall in London.

Jane was just 17 when McCartney saw her standing there (yep, that one’s about Asher) during a photoshoot in between rehearsal sessions. That night, after The Beatles finished their live set, Jane tagged along with the young band’s entourage. Paul and Jane wound up spending all night hanging out in his friend’s bedroom, but they kept it PG, talking into the morning about their favorite foods and making eyes at one another.

“I realized this was the girl for me,” McCartney recalled in an interview with the journalist Hunter Davies. “I hadn’t tried to grab her or make her. I told her, ‘It appears you’re a nice girl.’”

With Beatlemania growing and no hotel safe from crazed fans, McCartney soon all but moved in with Jane’s family at their townhouse on 57 Wimpole Street in London. He stayed in the attic of the six-floor flat and lugged that piano up the steps, just about filling up the space.

Paul McCartney and Jane Asher at the premiere of 'Alfie' at the Plaza Theatre, London on March 25, 1966

Paul McCartney and Jane Asher at the premiere of "Alfie" at the Plaza Theatre, London on March 25, 1966

It took months for McCartney to write the song

For the most part, at that point in time, McCartney and Beatles bandmate John Lennon wrote the band’s songs together, “nose-to-nose,” as Lennon would often tell interviewers. In a 2017 BBC documentary, McCartney gave some more clarity to their partnership: “I always used to write with John and it would be across from each other, either in a hotel bedroom on the twin beds, with an acoustic guitar and we’re just looking at each other.”

After the melody for “Yesterday” came to McCartney in a dream, he sprinkled some gibberish and some words about breakfast foods as placeholders for lyrics, just so he wouldn’t forget the tune. He played it for his bandmates and friends, first to make sure he hadn’t accidentally stolen it from somewhere else — thankfully, it was an original, they confirmed — and then to try to work out the rest of the song. In fact, McCartney played it so often on the set of The Beatles’ first movie, Help!, that director Richard Lester once threatened that he’d throw the piano off the set if McCartney didn’t complete it.

Lennon tried to help his pal with the song, but this was entirely a McCartney joint. Lennon's only contribution was the suggestion that the song title just be one word, but beyond that, he was just about useless. After months of struggling with the creative process, the lyrics suddenly came to McCartney in a very unlikely (and very inconvenient) place: driving down the winding hills of Portugal, where he was on vacation with Jane.

“It was a long hot, dusty drive,” McCartney told Miles. “Jane was sleeping but I couldn’t, and when I’m sitting that long in a car I either manage to get to sleep or my brain starts going. I remember mulling over the tune ‘Yesterday,’ and suddenly getting these little one-word openings to the verse.”

McCartney and Asher were going to stay as guests in the vacation villa owned by his friend Bruce Welch, who was also a musician. When they made it to the villa, McCartney rang up Welch and asked him if he had a guitar. Luckily, there was an acoustic guitar in the house, which made the arduous songwriting process just a bit easier. It took two more weeks to nail the lyrics… and then there was more waiting to do.

The 45 rpm cover of The Beatles singles "Act Naturally" and "Yesterday"

The 45 rpm cover of The Beatles singles "Act Naturally" and "Yesterday"

“Yesterday” almost wasn't released as a single

One of the reasons why The Beatles have resonated and stayed relevant for so many people for nearly six decades is that they were so musically daring, willing to change their sound as they lived through the trippy, tumultuous 1960s. But it took quite a while for the band to get so bold, with most of their more experimental songs coming out between 1966 and 1970. When “Yesterday” was written, it was still the middle of 1965, the band still had moptops, and changing their pop-rock formula seemed a little bit crazy.

As a result, their British label, Parlophone, declined at first to release “Yesterday” as a single, under the assumption that fans would find it strange and very un-Beatles-like. U.S. label Capitol Records had no such concerns and put it out with Ringo Starr’s western-inflected jingle “Act Naturally,” and the song became such a hit that Parlophone put out a short EP with “Yesterday” as its calling card.

In hindsight, it seems like an obvious hit, but even members of The Beatles weren’t so sure about its artistic merit. While best friends and songwriting partners, Lennon and McCartney maintained a healthy rivalry (or, Lennon felt one, anyway), and years later, Lennon revealed some mixed feelings about “Yesterday.”

“Although the lyrics don’t resolve into any sense, they’re good lines. They certainly work. But if you read the whole song, it doesn’t say anything,” he said in an interview that wound up in Playboy in 1980, just months before his death. “You don’t know what happened. She left and he wishes it was yesterday — that much you get — but it doesn’t really resolve.”

Regardless of Lennon’s opinion, generations of critics, musicians and fans alike have all agreed on the song’s brilliance. Rolling Stone and MTV both ranked “Yesterday” the top pop song of the 20th century and McCartney’s tune also holds the record as the most-covered song in history, with over 3000 versions and counting. Not bad for a little tune that came to Paul in a dream.

The Nine Lives of Ozzy Osbourne

"Biography: The Nine Lives of Ozzy Osbourne," premiering Labor Day, September 7 at 9/8c on A&E traces Ozzy’s life from his childhood in poverty and time in prison to fronting legendary Rock and Roll Hall of Fame inductees Black Sabbath and successful Grammy Award-winning solo career, to one of rock’s elder statesman and a loveable 21st-century television dad.