Facebook was supposed to be washed up by now. Four years ago, reports of a decline in usage by teens spurred a wave of predictions that it was headed the way of Myspace. Teens’ fickle taste was presumed to imply that no social network could keep its throne for very long.

Yet here we are in 2017, and Facebook’s grip on social media is stronger than ever. The company reported last week that Facebook itself is used by an astonishing 2 billion people each month. That’s close to twice as many active users as it had in 2013, when the doomsaying began.

Want to listen to this article out loud? Hear it on Slate Voice.

Perhaps more importantly for the company’s long-term future, two of its subsidiaries—Instagram and WhatsApp—are still growing at impressive rates in their own right. And much of that growth is coming from the same young demographic that was once seen as a threat to Facebook’s dominance. A recent study named Instagram the most popular app among U.S. teens age 13–17.

At the same time, the types of upstart rivals that once seemed destined to overtake Facebook are floundering. Twitter went public in 2013, a few months after the “Facebook isn’t cool anymore” narrative took hold. But its growth since then has essentially flatlined. As recently as nine months ago, industry watchers were touting Snapchat and its new augmented-reality Spectacles as a potential usurper of Facebook’s crown. Then Snap went public, and the hype balloon popped almost immediately. Now Twitter and Snap are the ones enduring gloomy warnings about their future obsolescence.

How did Facebook do it? How has it managed not only to stay on top of an industry that was thought to be inherently topsy-turvy, but to continually widen its lead? The answer is a simple yet devastatingly effective strategy aimed at neutralizing upstarts before it’s too late: If you can’t beat them, buy them. And if you can’t buy them, copy them.

Facebook embarked on this strategy even before most industry watchers thought it was in any danger. In 2012, it acquired Instagram, the minimalistic photo-sharing app popular mainly with young people, for what seemed at the time to be an astronomical price: $1 billion. This despite Instagram having just a handful of employees, zero revenue, and no obvious path to profitability. The startup had been valued at an estimated $500 million just a few weeks earlier.

The move prompted a flurry of rage-quits from Instagram users who had viewed the service as a refuge from the increasingly corporate, adult-dominated social network. Some commentators praised it as farsighted; others criticized it as reckless. I did some of each, arguing that the move made sense on its own terms but signaled a broader strategy that might prove unsustainable. I concluded:

Mark Zuckerberg can only buy so many $1 billion companies, and thanks to the app stores built by Google and Apple, the supply of tiny, potentially revolutionary startups is endless. If it keeps on spending like this, even Facebook will eventually run out of billions.

Facebook’s strategy, it turned out, was more nuanced than I gave it credit for. The supply of potentially revolutionary startups may indeed be endless—Yik Yak, Ello, Meerkat, and Peach are just a few of those that have gained attention as would-be Facebook killers over the years, only to flop soon after. Yet the company has proved to be as selective in its choice of targets as it is aggressive in pursuing them.

In some cases, it has simply ignored would-be rivals, confident in the power of its network to fend off a challenge from a direct competitor. This is especially true in the case of the “anti-Facebooks”—social networks whose core features mimic Facebook’s own, such as Google Plus, Diaspora, Ello, Peach, and the many reincarnations of Myspace.

Facebook tends to pay much closer attention when a startup attracts large numbers of youngsters by offering a social experience substantially different from its own. Instagram may have only had 30 million users in 2012, but they weren’t just users—they were addicts. They loved the simplicity of uploading and sharing photos on their phone, a functionality that Facebook itself had failed to master. (It was around that time that the company realized that it would be doomed if it couldn’t make the leap from users’ desktops to their smartphones; Facebook’s subsequent shift to mobile is one of the great business success stories of the era.)

In recent years, a select handful of other social media platforms have worried Facebook enough to prompt it to significant action. They include YouTube, Vine, Periscope, WhatsApp, and Snapchat. Each one brought a fresh element to online communication that was lacking from Facebook at the time: original video, looping videos, personal streaming video, group messaging, self-destructing messages, curated personal “stories.” The ones Facebook couldn’t buy—Google’s YouTube, Twitter’s Vine and Periscope—it blatantly copied instead, either within Facebook or one of its subsidiary apps, such as Instagram or Messenger. WhatsApp, with its loyal network of 430 million users around the world, might have seemed too big to buy. But Facebook wasn’t satisfied with its efforts to copy it via Messenger, so it went ahead and paid an enormous premium—$19 billion—to acquire it anyway. Three years later, WhatsApp has more than doubled in size, and it has helped to make Facebook the leader in messaging—a popular and still-growing category among teens.

Of all the would-be Facebook rivals, none more perfectly embodies everything that keeps Mark Zuckerberg up at night than Snapchat. Its users are overwhelmingly young; its growth has been meteoric; it is cool and insouciant and confusing to adults in a way Facebook may have once been, but certainly never will be again. On top of that, it has proved wildly innovative, pioneering a series of products that have changed how people interact.

In Snapchat’s case, Facebook tried first to buy it for $3 billion—again, far more than most observers thought it was worth. But Snapchat’s Evan Spiegel wouldn’t sell, much the same way Zuckerberg repeatedly declined seemingly generous overtures from the likes of Yahoo and Microsoft on his way to surpassing them both. So Facebook resorted to Plan B, relentlessly copying Snapchat’s successful features. Here, too, it failed repeatedly—remember Poke? Or Slingshot? But it never stopped trying.

For all its efforts, the main Facebook app has yet to successfully copy a major Snapchat feature. Something about either the team’s approach, the Facebook brand, or the structure of its network (in which kids are inextricably linked to their parents and other authority figures) has made it helpless to recapture the interest of Snapchat’s young users. This was exactly the sort of scenario those doomsayers had in mind back in 2013. It’s hard to say exactly how much share of teens’ attention Facebook has lost over the past five years, since the company doesn’t break out usage metrics by age group. But in a 2016 Business Insider survey on teens’ favorite apps, the big blue one didn’t even make the list.

Here’s the twist: Core Facebook hasn’t been able to fend off Snapchat, but its subsidiaries have. In the space of nine months, Facebook copied Snapchat’s “Stories” feature on no less than four of its platforms: Instagram, WhatsApp, Messenger, and Facebook. The first two—Instagram Stories and WhatsApp Status—have already eclipsed Snapchat’s original in active users. This is not just a case of Facebook’s oldsters discovering the form anew: Reports suggest that Instagram Stories in particular are directly siphoning both users and stars from Snapchat. And now Snap is the one facing pressure from investors to show that it can survive the onslaught from Facebook.

In retrospect, the Facebook doomsayers underestimated both the company and the severity of its coolness problem.

Historically, big mergers and acquisitions in the technology sector have often been disastrous (think HP/Compaq, AOL/Time Warner, and pretty much any startup Yahoo ever acquired). Facebook managed to buck this trend by seeking not “synergies” or “cost savings,” but young and fast-growing user bases. And it was willing to strategically overpay to get the ones it targeted. Meanwhile, it proved adept at mimicking the best features of those apps it wasn’t able to acquire.

At the same time, it turned out being hip wasn’t actually the key to dominating social media. In a recent survey of millennials, Facebook, Instagram, and WhatsApp all ranked relatively low in “coolness” compared with other tech brands, including Snapchat. But Zuckerberg has always cared less about being cool than about being massive, and he has discovered that the latter doesn’t necessarily depend on the former.