As a high school sophomore in the 1990s, I took a mandatory computer science class that had a reputation for being difficult. The word among girls was that the only students who did well in the course were the “Dungeons & Dragons boys.”

A very nice teacher taught the class. But he often reinforced this male-oriented image of who could be successful with nerdy Star Trek jokes and other pop-culture references more likely to resonate with boys than girls. Unsurprisingly, boys dominated classroom interactions, answered questions confidently and turned in their tests quickly. Many of those boys went onto become computer scientists and engineers—while girls largely turned to fields like social sciences, medicine and business.

The same dynamic persists on a broad scale across the United States. Although women have made great strides in STEM fields like biology, chemistry and math, a large gender gap persists in computer science, as well as engineering and physics. In fact, just 18% of undergraduate degrees in the subject go to women today, down from 29% back when I was in college.

A recent psychological study I conducted with my colleagues, published this month in the Psychological Bulletin, has a clear message about our best hope for diversifying computer science. To draw more girls into STEM fields, it’s not enough to provide more learning opportunities. The cultures in these fields need to change to communicate to girls that they belong in them just as much as boys do.

We analyzed over 1,000 research articles to determine what distinguishes the more gender-balanced fields— biology, chemistry and math — from computer science, engineering and physics, which have greater gender disparities. We found that the difference comes down to culture. Computer science, engineering, and physics have more masculine cultures than biology, chemistry, and mathematics, and that these masculine cultures are turning women away. Specific factors include male-oriented stereotypes about the people in the fields, stereotypes that women have lower abilities, and a dearth of female role models.

When courses are optional, as is typical for computer science, students rely on their stereotypes about the fields to decide whether to enroll. And as one undergraduate research participant in our lab put it, the current stereotypes of computer scientists is that they are “nerdy guys” who “stay up late coding and drinking energy drinks” and have “no social life.”

When high school girls see Star Trek posters and video games in a computer science classroom, they opt out of taking the course. This geeky image is at odds with the way that many girls see themselves. Work from our lab shows that when high school girls see Star Trek posters and video games in a computer science classroom, they are less interested than boys in taking the course. When the classroom is devoid of décor, girls still opt out. It is only when an alternate image of computer science is presented by replacing geeky objects with art and nature posters that girls become as interested as boys.

All this matters a great deal because optional courses not only reinforce current gender divides—they magnify them. Because boys are more likely to opt in to pre-college experiences with computer science, when they get to college, they dominate introductory courses. Girls who come in without the same knowledge tend to believe they are worse at computer science and not cut out for the field.

The research is clear on what works to encourage more girls to choose computer science: change the masculine culture. Changing the stereotypes by diversifying computer science environments is one way to reduce gender gaps.

Our research has also found that college women who have a two-minute interaction with a computer science major (played by student actors) who wear t-shirts that say “I code therefore I am” and identify Mystery Science Theater 3000 as their favorite show express less interest in majoring in computer science than college women who interact with the same students wearing regular t-shirts and watching The Office instead. Computer science teachers who showcase the ways in which their personalities and interests are broader than the narrow stereotypes may have an easier time convincing girls to follow in their footsteps.

Culture change isn’t always easy, but it is always possible. The University of Washington’s computer science department has been working hard for the past decade to create a more inclusive culture for women. The department added art and nature posters throughout their building to make it more inviting. Women were appointed as teaching assistants in many courses. Professors sent personalized emails recognizing women who received high grades in introductory courses. These emails defined success as getting good grades, rather than whether one plays video games or knows science fiction references.

The efforts have paid off. Last year, the proportion of undergraduate computer science degrees going to women at the University of Washington, 32%, was higher than any other public flagship university in the country. The gender gap began to close once women could learn computer science in a culture that signaled they belonged.

It’s time we take the pressure off women to change themselves to fit within masculine cultures. Instead, the pressure should be on society to make computer science a field in which all students feel equally welcome.

Image credit to WOCinTech Chat via CC 2.0. We welcome your comments at ideas@qz.com.