Mary had a little lamb. This much any child could tell you.

But what’s less remembered is the whole story of what happened to Mary and her lamb. Though the poem’s origins are a bit murky, writes Elizabeth Periale for the Smithsonian Libraries blog, it’s generally accepted that the poem was “based on an incident in the life of Mary Sawyer, of Sterling, Massachusetts.”

As documented by poet Sarah Josepha Hale, the story, published on this day in 1830, goes like this: Mary’s lamb, unnamed, follows her everywhere, and follows her to school one day. But that’s not the end. Lambs at school being frowned on by the educational establishment, the teacher kicked him out. But Mary’s little lamb waits for her outside the school. “What makes the lamb love Mary so?” her classmates ask. “Mary loves the lamb, you know,” the teacher replies, saying that kindness to animals will gain their loyalty.

But according to the New England Historical Society, the story goes deeper than that. Sawyer recalled later in life that she persuaded her parents to allow her to hand raise the lamb after its mother rejected it. “At first the creature could not swallow,” she said, but with her devoted nursing the lamb recovered and started following her everywhere. She remembered:

The day the lamb went to school, I hadn't seen her before starting off; and not wanting to go without seeing her, I called. She recognized my voice, and soon I heard a faint bleating far down the field. More and more distinctly I heard it, and I knew my pet was coming to greet me. My brother Nat said, "Let's take the lamb to school with us."

So she hid the lamb in a basket at her feet. But the teacher caught on after it bleated. Then the teacher, as recorded in the poem, put the lamb outside.

The first version of the poem wasn’t written by Hale, writes the Society, but by one John Roulstone, who was there that day and gave Mary the poem. Fourteen years after that, Hale added three stanzas incorporating the moral lesson, the Society writes, and published the poem in her book.

Sawyer even capitalized on her fame later in life, writes the Society—if for a good cause. A group of locals was raising money to restore the Old South Meeting House, a historic building in Boston, the Society writes. Mary donated the stockings that her mother made for her from the pet lamb’s wool years earlier. “The stockings were picked apart and wool was attached to cards that said, ‘knitted wool from the first fleece of Mary’s Little Lamb,’” the Society writes.

The poem has lived on as a child’s song. In 1877, Thomas Edison “shouted” it in one of the earliest recordings, writes Lisa Brenner Katz for 89.3 KPCC.