Blame stress, a snoring partner, or the neighbor’s nocturnal and barky dog. There are hundreds of reasons why you—and an estimated three in 10 other Americans—have trouble sleeping. And, like a recurring nightmare, insomnia and other snoozing issues tend to get worse the longer you have them.

“People get really good at not sleeping well,” said Dr. Abhinav Singh, a sleep medicine expert and the author of the forthcoming book Sleep to Heal: 7 Simple Steps to Better Sleep. “You lay down, you can’t doze off, and then you look at the clock and the behavior repeats itself.”

To make things worse, according to a recent study by the American Academy of Sleep Medicine, more than half of survey respondents who had trouble getting winks turned to potentially habit-forming drugs (from over-the-counter potions like Unisom and ZzzQuil to Ambien and other prescription medicines).

You can get better sleep. It might help to understand that, while there are many reasons you often can’t doze off quickly or wake up in the middle of the night, they fall into just three categories: external, internal, and mental.

“External are unfavorable elements in your environment, from outside lights, a bad mattress, or someone next to you snoring. If things aren’t ideal, you might not sleep,” said Singh.

External factors can also be bad habits you inflict on yourself, such as drinking caffeine shortly before bedtime or scrolling through all the crummy news and drama served up by Twitter before you tuck in—or after you wake up at 3 a.m.

Internal factors include ailments like arthritis pain, sleep apnea, fluctuating hormones, and restless leg syndrome—basically anything happening to you that makes slumber more difficult.

But the most challenging foe of drifting off into deep, sweet dreamland might be your own mind. “If you’re a good sleeper, you don’t have to do anything; you just lie down and doze off,” said Jamie Zeitzer, a professor of psychiatry and behavioral sciences at Stanford University.

If you develop insomnia, then it can be a lifelong struggle against the little voice in your noggin saying, “I can’t get to sleep, I can’t get to sleep.”

Here are 40 tips to help you drift off and stay asleep.

Become a Smarter Consumer Get free, expert advice delivered to your inbox every Wednesday when you sign up for the Weekly Checklist newsletter.

Block Out the Light

1.  Don’t be afraid of the dark. Sleep is a function of your circadian system—the 24-hour cycle your body goes through that tells you when to go to sleep or to get up. Because it’s tied to sunrise and sunset, it’s crucial that you turn down the lights to horror-movie level in your boudoir. “The darker, the better,” says Singh. “Studies show that even tiny bits of light can interfere with sleep.” That means you should…

2.  Put up blackout shades or curtains. Hotels have the right idea with those walls of drapes you can use to shut out the Vegas morning sun or the streetlights outside your motel right off the interstate. Click here for our latest advice, ratings, and price comparisons of window treatment stores.

3.  Address other sources of light pollution. Get rid of always-on digital clocks and adjust smart thermostat settings so the display goes dark. Use electrical tape to seal off small beams of light from internet routers and other devices. If your kids need a hall light kept on, install a door sweep to block it from bleeding into your room.

4.  Buy a sleep mask for afternoon naps, travel, or if there’s no practical way to block bothersome light. A mask can also be handy for shutting out glow from a nearby device or lamp if your partner is a late-night reader or doom scroller. (On an airplane, you can sub in sunglasses.)

5.  Hello, sunshine. The amount of light you get during the day—particularly in the morning and early afternoon—can impact how awake you feel and help prep you for sleep once it gets darker. Studies show a little sun or natural light for at least 30 minutes a day can help you doze off more easily later.

Get Comfortable

6.  Make sure your mattress is comfortable. Whether that means you prefer firm or soft, foam or springs, what’s important is that your mattress cradles you in whatever your idea of comfort is. If you and your partner disagree on firmness or type, buy separate ones or an adjustable model.

Consumer Reports regularly performs rigorous testing of mattresses, judging them for comfort and value. Here at Checkbook.org, you’ll find ratings of mattress stores. Be warned that many retailers make comparing mattresses difficult, using private labeling, fake sales, and shifty fees to confuse customers and prevent meaningful price comparisons. There are now several mail-order-mattress options—with demand largely fed by so many local stores making mattress purchases so unpleasant. But we still think it’s best to visit local stores to flop down on several models at wide price points before you buy.

7.  Pick your perfect pillow. CR rates pillows, too. And most stores or websites that sell them offer buying guides to help you choose the right place to rest your head by whether you’re a side, back or faceplant sleeper.

8.  Keep your boudoir cool. Toward bedtime, your body temperature drops to bring on sleep; if you keep your bedroom chilly, that helps, too.

9.  Snuggle under breathable sheets and blankets, which means natural fibers. A down comforter is often the best option for most. Then add a few more light layers that you can peel off easily if you get too toasty.

10.  Regularly wash your bedding. Dust and dirt can make your sheets feel itchy.

11.  Clear the air. Purifiers and humidifiers can improve indoor air quality and reduce nasal itchiness, runny noses, and coughing. But don’t have your ductwork cleaned. As we’ve noted, it provides little measurable benefit, even if your ducts are very dirty.

Nix the Noise

12.  Use earplugs to block out noise from snoring spouses (and pets), car alarms, zombie hordes, or whatever is making a racket and keeping you up. My husband snores at punk-rock-concert-level decibels and I swear by the silicone variety that you use to cover, not stuff, your ear canals.

13.  White-noise machines and smartphone apps create a wall of sound (surf, crickets, birds, wind, or just boring noise). Even running a simple fan might help drown out a racket.

14.  There’s much you can do in and around your home to lower unwanted noise by installing sound blocking and absorption materials, from inexpensive door sweeps and acoustic panels to pricey projects like replacing doors and windows. Click here for our tips on soundproofing your pad.

15.  Shut off phone alerts. It’s obvious, but we’ve all been jolted awake by pinging or buzzing. Do Not Disturb mode is your friend.

Stop Snoring

16.  If you or your partner snore, consider asking your primary care doctor about getting a sleep study to determine whether the cause can be easily cured.

Simple studies can be done using a take-home device that will record various data. In some cases, you’ll still have to bunk at a sleep center overnight.

Tests may find that you have a deviated septum, sleep apnea, or other condition that impacts your breathing and sleep, as well as others’ sleep. Then you can address that issue.

17.  If you have sleep apnea, you might need a CPAP [continuous positive airway pressure] machine to help you breathe at night. While some research suggests these devices are overprescribed in the U.S., a lot of users report improved sleep—and marriages.

18.  Another contraption that might stop snoring? A mouthguard. Some dentists will prescribe them, but, increasingly, once your primary care doctor indicates you need one, you can order one online yourself.

There are two types of mouthguards; both resemble orthodonture retainers.

The first, mandibular advancement devices (MADs), are generally molded to fit your teeth and keep your jaw in place. They work by opening your airways which, in theory, leads to reduced snoring.

A tongue retaining device (TRD) or tongue stabilizing device (TSD) is also an option. These contraptions also fit between your choppers, but they keep your tongue in place using suction. A National Library of Medicine study found that they reduced snoring in 68 percent of subjects.

19.  Try other snore solutions. Nasal strips can help keep your breathing passages open; there are also anti-snore pillows and sprays that can lubricate your throat. Do these products always work? Probably not, but they’re inexpensive and worth a shot.

20.  Some snorers, particularly back sleepers, find that using a firmer pillow or propping up on two or more pillows helps them keep airways open enough to eliminate snorts and ZZZZzzzzzs.

21.  Make lifestyle changes. The National Sleep Foundation, a nonprofit sleep education and advocacy group, counsels skipping drinking alcohol shortly before bed and losing weight to see if either reduces your snoring.

Adopt Healthy Habits

22.  Cut down on caffeine. The amount individuals can have (and when) and still get good rest varies. But if you’re having trouble dozing off or staying asleep, “put the brakes on,” says Singh. “The U.S. is so obsessed with caffeine; I think sometimes people don’t really know how much they are ingesting.”

23.  Don’t eat too close to bedtime. Your body will spend energy digesting chow instead of getting sleepy.

24.  Don’t overhydrate close to bedtime, for obvious reasons.

25.  Booze, particularly in excess or just before bedtime, hampers a good night’s rest. You might initially pass out in an F. Scott Fitzgerald-like stupor only to wake at 3 a.m. with a dry mouth and headache. Skip the stuff altogether or stop drinking two to three hours before you want to get shuteye.

26.  Smoking can also interfere with your ability to get good sleep. Quit or don’t puff close to bedtime.

27.  Work out! Numerous studies support the hypothesis that exercise during the day can help you fall asleep faster as well as keep you from waking up in the middle of the night. Moderate aerobic activity increases the amount of slow wave or deep sleep that you get.

28.  However, when you exercise matters. Studies also support that aerobic exercise later in the evening (usually two to three hours before bedtime) releases endorphins and contributes to wakefulness.

29.  Try to limit screen time within an hour or two of bed. The blue light from computers, smartphones, TVs, and their ilk can interfere with sleep. This happens because blue light impacts levels of the sleep-inducing hormone melatonin.

Don’t Take Too Many Chill Pills

30.  There are numerous pills and potions that promise to help you sleep better. Some are potentially addictive and only safe to use short term; others can become part of your nightly routine. Still, says Singh, “these things are really only training wheels—you need to learn to sleep better on your own.”

31.  Melatonin, which your body produces naturally to signal it’s time to sleep, can help you doze off. It’s available over the counter, but you should still consult a doctor before using it.

32. Sleeping pills and liquids provide more of a knockout effect. Pop an Rx-only one (Ambien, Edluar, Intermezzo), and it’ll help you sleep by sedation or a hypnotic effect. Over-the-counter liquids and pills (ZzzQuil, Unisom, etc.) use the antihistamine doxylamine succinate to make you sleepy.

Both varieties of sleeping pills carry risks of grogginess and impairment, which are heightened if you take them with alcohol. Ambien and other prescription sleeping pills can also be addictive if used for an extended period.

33.  If you’ve got chronic insomnia, ask your doctor about a newer class of insomnia drugs: dual orexin receptor antagonists (DORAs). Sold under the brand names Belsomra, Dayvigo, and Quviviq, these medications block receptors in your brain that help you stay awake, especially at night. Because DORAs don’t force you into unconsciousness and aren’t considered addictive like sedatives, scientists think that the sleep they induce is closer to normal.

34.  Also check whether any existing meds are robbing you of good sleep. Many prescription and OTC drugs can affect sleep patterns. Review with your doctor what you’re taking and when, and discuss whether you should make changes to what you take and when.

Establish a Routine and Stay Positive

35.  Some pros call setting a sleep schedule and routine “sleep hygiene.” It essentially means that you try to go to bed and wake up at similar times each day. It also means that you identify, via trial and error, what helps you zonk out easily and what makes you stay up counting sheep.

For an early bird, it might mean rising with the sun and downing precisely two cups of java, then going to bed at 9 p.m. after 10 minutes of slow yoga and a cup of chamomile tea. Night owls might sleep in and stay up reading thrillers until 11 p.m. before tucking in. Go for whatever pattern helps you get enough shuteye. Many experts recommend keeping a sleep journal, which can help you figure out what works and what doesn’t.

36.  Don’t toss and turn. If you can’t get to sleep after 20 minutes in the dark, read a book (preferably not a blue-lit e-reader), stretch, or have a glass of water.

37.  Slow down your breathing. It will help lower your heart rate and blood pressure, which can facilitate sleep.

38.  Try a relaxation app. There are scads of options, including Calm, Hatch, Headspace, Sleep Cycle, Sleep Easy, Sleep Reset, and Yours, which can play soothing music, sounds, stories, or guided breathing and meditation exercises. Some apps are free; some cost $5 to $10 per month. Most usually offer free trials for a week or two so you can figure out which one (if any) works best for you.

39.  Think happy thoughts. Relaxation apps might also help shift your mind away from distractions and toward stress-free vibes. Or simply practice recalling enjoyable vacations or other past experiences.

40.  Don’t beat yourself up. As we mentioned in the intro, our own minds are often the biggest obstacles to getting to sleep and staying that way. Try not to think of the process as a must-do task—that can bring on performance anxiety or frustration. Know that you’re not alone; insomnia is quite common. Try to remain relaxed and positive.