Sometimes the dreaded all-nighter just can’t be avoided. Maybe you have a new job working night shifts, it’s finals week, or you’re having a sleepover party. Regardless of your reasons, staying up all night is tough.

Human sleep patterns follow natural circadian rhythms. Your circadian rhythms are like internal clocks affecting the way you think, feel, and behave throughout the day. Circadian rhythms are based on the lightness or darkness of your environment.

When your brain perceives darkness outside, your body begins to release a hormone called melatonin. Melatonin makes you drowsy and prepares your body for sleep.

Staying up all night means fighting this natural process, which is not only difficult, but also unhealthy. Sleep deprivation can impact your ability to learn and focus. It can even be dangerous. In 2013, there were at least 72,000 car accidents caused by drowsy driving.

If you must stay up all night, the following tips can help you do it safely.

1. Practice

The easiest way to stay up all night is to reset your internal clock. This can take up to one week, but it’s possible. You may experience serious drowsiness at first, but your body does catch on.

If you’re switching to the night shift, give your body a few days of practice. Your circadian rhythms still rely on light cues, so make sure you’re sleeping in a very dark room during the day. Blackout curtains and eye masks are particularly helpful.

2. Caffeinate

Caffeine is a helpful pick-me-up and can increase your alertness. It helps fight one of the natural substances your body releases to make you drowsy.

Studies have found that moderate doses of caffeine (600 milligrams [mg] or more than four cups of coffee) can improve your ability to think and perform tasks, but high doses (900 mg or more) have the opposite effect. High doses of caffeine can cause symptoms like anxiety and shakiness that make it harder for you to concentrate.

To stay up all night, don’t rely on one big dose of caffeine. Too much coffee can lead to stomach upset. Instead, try taking several smaller doses throughout the night such as espresso shots, caffeine pills, or caffeinated gum.

3. But avoid energy drinks

Energy drinks contain varying amounts of caffeine, typically the equivalent of one to five cups of coffee. They also contain guarana, an ingredient that also contains caffeine, which makes the total amount of caffeine higher than it appears.

When using energy drinks, it’s difficult to know exactly how much caffeine you’re ingesting, and extremely high doses of caffeine can be toxic. They’re especially dangerous when mixed with drugs or alcohol. In 2011, more than 20,000 people went to the emergency room because of energy drinks.

4. Take a nap

Taking a series of small naps throughout the night may help you stay alert. Although it’s not equal to a full night’s sleep, short naps can be restorative. Most studies on night-shift workers find that naps reduce sleepiness and improve performance.

Try to catch 15 to 20 minutes of sleep during a break. If you’re driving through the night, pull into a rest stop for a quick nap.

5. Get up and move

Daily exercise helps you maintain a healthy sleep schedule, but experts recommend avoiding exercise late at night, if you want to sleep well at night. That’s because your body produces a lot of energy when you exercise, which can keep you awake.

If you’re trying to stay up all night, try 30 to 40 minutes of aerobic exercise. If you don’t want to exercise, try getting up and moving around. Pace back and forth for 10 minutes, take a walk outside, or do a few jumping jacks.

6. Find some bright lights

Darkness cues your body to release melatonin, a hormone that makes you feel sleepy. One study found that using bright lights at night and creating darkness during the day can help night-shift workers reset their circadian rhythms.

Find a lamp that can distribute light widely throughout the room. Look for an LED bulb that can simulate sunlight. This should help you stay awake longer.

7. Use your devices

Your electronic devices, including laptops, tablets, TVs, and phones, emit something called “blue light.” According to the National Sleep Foundation, the blue light emitted from your devices can delay the release of melatonin, the sleep hormone. This can prevent you from becoming sleepy.

To keep yourself awake, use a device that you can interact with. Try playing video games on your computer or tablet. The closer the blue light is to your face, the more awake you will feel.

8. Take a shower

Taking a cold or lukewarm shower can help wake you up when you start to get tired. If you don’t want to shower, splashing your face with cold water can help. Brushing your teeth can make you feel refreshed.

Catch up the next day

Staying up all night isn’t good for you and should only be done as a last resort. After staying up all night, you’ll feel very drowsy. Try to make up the sleep the next day.