It’s so easy to end up in a bad mood when someone close to you is feeling down. Being there for our friends, family, and partners when they’re going through a hard time is really important, especially if they’re experiencing something genuinely traumatic, like the loss of a loved one. On the other hand, we all have at least one friend who throws a helluva pity-party when they’re just not feeling good about themselves or the world around them.

When our friends are down—whatever the situation is—it’s also critical that we take care of ourselves. It can be hard to take an emotional step back when people close to you are going through a funk, but once you’re sucked into that black hole of negativity, it can be even harder to fight your way out.

Emotions Really Are Contagious

Ever wonder why someone else’s moods can affect you so much? A 2017 study found that teens who surrounded themselves with negative friends also found their moods to worsen over time, a process known as social contagion.

“Scientifically, we talk about the mirror neurons in the brain that are purposely created so we can be empathically able to experience what someone else is feeling,” says Kate Dow, Ph.D., a psychologist and certified wellness coach for women. “The challenge is if you are a very sensitive person, that empathy becomes an open door to taking on other people’s feelings and not being able to have a sense of self to hold onto.”

“It’s the way we’re wired,” says Jonathan Alpert, psychotherapist and author of Be Fearless: Change Your Life in 28 Days. “We try to connect to people, and we do that first by picking up on how they feel and then bringing a level of understanding and support.”

So when your Facebook friend from high school decides to post for the tenth time today about how much her life sucks, or when your coworker is counteracting everything you say with a negative remark, here are some tips from life experts to keep your sanity intact.

1. Acknowledge your funk.

If you’ve fallen into negative thinking because of your friend, the first step toward a positive mindset is consciously accepting that you’re currently in a state of negativity. “Knowing that you’ve fallen into it is a huge advantage,” Dow says.

2. Give yourself a pep talk.

If you know you’re going to see someone who’s in a bad place emotionally, prepare yourself before you interact with them. Dow suggests giving yourself a pep talk before going in—one that acknowledges the fact that you’re going to face this person, that they will be upset, and end with an affirmation stating that you will choose not to take on their emotions. This way, you can have more perspective on your friend’s situation and you’ll give yourself more of a choice about whether or not to be upset, Dow says.

Try pushing the negative self-doubt away by giving yourself a compliment.

3. Get your friend out of their head.

If you’re stuck hearing about your friend’s frustration over their boss and how everything is going wrong for them, your initial reaction may be to nod in agreement. But Alpert suggests a different route: Allow your friend to vent for a few minutes, then redirect.

“If someone is complaining all the time and you’re agreeing with them, you’re reinforcing that behavior, and that may not be so healthy,” says Alpert. Offer an alternative way to look at solutions, such as discussing what’s going well in their life or a shared interest.

Of course, this advice is only good for smaller irritations—if your friend is going through something life-altering, it’s good to let them talk about their feelings as much as they may need to.

4. Set boundaries.

“We only have so much we can give to people,” Alpert says. “Make sure you’re taking care of yourself and your needs are met.” When we get wrapped up in friends’ and loved ones’ drama, we can forget about ourselves. But when you’re at your wit’s end with your pal, setting time apart could be what heals your friendship. Focus on other activities you love or spend time with other people in your life. “Not hanging out with them isn’t about being mean or judgmental,” Dow says. “It’s self-care, and ultimately, it’s each of our responsibilities to ourselves.”

5. Step away from technology.

Being connected at all times has its downsides, and if you’re dealing with your BFF’s issues at 11 p.m., you’re setting yourself up for problems. Try turning off your phone, removing social media apps, or even deactivating accounts until you’re feeling better. “If you don’t take a break, your brain and body are experiencing high-stress stakes constantly, and chronic stress can lead to getting sick,” Dow says.

6. Show gratitude.

A gateway to a positive mind starts by appreciating what you have. In fact, a study performed at the University of Miami found a link between gratitude and happiness. Two groups wrote something every day about their lives: One group focused on things they were grateful for; the other, their irritations. The participants who wrote about gratitude felt happier and better about themselves after ten weeks than the group who focused on griping. And like negativity, gratitude spreads: Another study found that couples who expressed gratitude for one another had more loving, trustworthy relationships.

7. Practice being kind to yourself.

We’re our worst critics, and once we’re in a bad mood, we can’t help but continue to beat ourselves down. Try pushing the negative self-doubt away by giving yourself a compliment. “Positive focus helps support our positive mindset,” Dow says. She suggests setting an intention every day promoting a healthier, kinder attitude.

8. Reframe your thoughts.

If you catch yourself using a lot of negative phrases and bringing yourself down, try looking at the bigger picture. “Repeating negative narratives is really going to put someone in a funk,” Alpert says. So be kind to yourself—and change the narrative.

9. Consider whether this is someone you want in your life.

No one likes to break up with a friend, but if someone is bringing a lot of negativity into your life—or if you suspect they may be toxic—you should reevaluate whether or not you want to spend time with them. It’s an extreme case, but at times, it’s necessary. Figure out how much this friend means to you and how important it is to maintain that friendship, Dow says.

Bianca Mendez is a writer in New York covering health, sex, relationships, and beauty. Follow her on Instagram @biancammendez and Twitter @biancamendezz.