When Reddit co-founder Steve Huffman returned to the company as chief executive in 2015, it wasn’t exactly a triumphant return.

“It was really, really tough,” he said. “The thought process I had at the time was it would be a real shame if Reddit didn’t live up to its potential or, even worse, ceased to exist.”

At the time, Reddit was considered one of the worst corners of the internet. You could find anything from violently racist forums to child pornography. While it’s still not entirely “safe for work” now, it’s a lot less scary. Advertising revenue has started growing again, and the story of how a sinking ship righted itself could offer insights for other other social media sites trying to clean up.

Huffman launched Reddit in 2005 and like so many social media companies’ founders, he had an utopian vision: total freedom of speech. A site divided into communities where you could find like-minded people talking about pretty much anything. One group dedicated to epiphanies people have in the shower, for example, has 19 million members. Another is dedicated to pictures of cats that look like loaves of bread.

At that time, toxic communities also began flourishing, according to Christine Lagorio-Chafkin, author of “We Are the Nerds,” a book about Reddit’s history. She says parts of the site were “filled with hate, harassment, bullying and a lot of behaviors that when taken as one individual post might not seem that bad, but once a community forms around it … clearly showed a picture of a kind of depravity.”

But Huffman says the site’s community structure made it easier to shut down the worst conversations. It’s less of a challenge to take down an entire group that self-identifies as racist than it is to police individual users on the loose. That’s a problem sites like Twitter face.

“It’s our greatest strength. The communities that end up getting banned or quarantined do us a favor and all stand in one place for us,” Huffman said. “That makes it really convenient.”

Huffman banned some offensive communities and “quarantined” others — only members could see them in search results and advertisements were removed. Not that there were a lot of ads back in 2015.

“We were not burdened with advertisers,” Huffman joked. “Because nobody wanted to talk to us.”

The strategy has achieved results. “Those cleanup efforts have not only helped some brands feel more comfortable with advertising on Reddit, but have also brought in some new users,” said Jasmine Enberg, an analyst with eMarketer.

Reddit now has 430 million users, up 30% on last year. Advertising revenue is growing, but Reddit’s ad sales are still quite small compared to sites like Twitter. Reddit made roughly $119 million from ads last year, up more than 50% on the year previous, according to eMarketer. Twitter, with fewer users, made $702 million on ads in the last quarter alone.

Huffman is optimistic about increasing ad revenue.

“I think we’ve paid down a lot of that debt. And have dramatically repaired our reputation,” he said. “It was one pitch at a time, one advertiser at a time. And as we’ve grown it’s become easier and easier.”

But there’s been a lot of questions about why some communities don’t get shut down more quickly. The group “Watch People Die” is exactly what it sounds like. It was quarantined, but Reddit didn’t remove it until the group posted video footage of last year’s massacre at a New Zealand’s mosque.

One of the groups that’s still quarantined is “The Red Pill.” It’s full of misogynistic posts, talking about ways to pressure women into sex or how women are genetically inferior to men. Why not remove it entirely?

“Our job at Reddit is to provide a reflection of the world,” Huffman said. “And not to provide a squeaky clean, sanitized view of the world.”

As long as social media sites profess to embrace free speech, there’s probably no danger of that kind of unsavory content disappearing.