Andy Beshear just flipped Kentucky’s governor’s seat from red to blue, and he did it with an army of public school teachers behind him.

Hundreds of teachers made phone calls, knocked on doors, and offered voters a ride to the polls. They organized get-out-the-vote programs and “Bevin is a Bully” events (deriding incumbent GOP Gov. Matt Bevin).

While the teachers’ actions were driven more by their intense hatred of Bevin than by pure enthusiasm for Beshear, it still worked. On Tuesday, by a slim margin of 5,300 votes, Beshear ousted the Republican governor of a deep red state. Even President Donald Trump’s rally to support Bevin the day before wasn’t enough to save him.

In a private Facebook group, Kentucky teachers rejoiced as the results came in Tuesday night.

“I’m so proud of us!” a school librarian commented on the page of Kentucky 120 United, a grassroots group of 7,000 teachers and educators who organized the teachers strikes in 2018.

“Everyone should wear blue tomorrow because we turned KY blue,” wrote another member.

Eddie Campbell, president of the Kentucky Education Association, said he’s never seen teachers so engaged in the political process. About 1,000 members volunteered on Beshear’s campaign, he said.

“The case they made to their communities changed the course of this election and the course of public education in this state,” Campbell told me Wednesday. His organization represents more than 45,000 current and retired educators, and many aspiring teachers.

Kentucky teachers don’t get all the credit for Beshear’s win, though. Bevin is largely to blame for his own loss (as of press time, he still hasn’t conceded the race). He is one of the most unpopular governors in the country, with a 51 percent disapproval rating. And he incensed teachers last year when he tried to cut their pension benefits and public school funding. The teachers strikes that followed helped seal his fate.

Bevin’s budget cuts and insults riled up teachers

In January 2018, Bevin proposed a state budget that would have required school districts to cut costs by 12 percent, and would have slashed $16 million in funding for new textbooks and other classroom materials. That angered the Kentucky Education Association union.

Then, in April 2018, Republican lawmakers quickly passed a bill, which Bevin signed, that cut pension benefits for new and retired teachers. Teachers flipped out — 5,000 of them walked out of class to protest outside the state capitol in Frankfort.

Later that month, teachers went on strike again, temporarily shutting down every public school in the state after Bevin vetoed a two-year budget that would have boosted school funding by $480 million through various tax hikes.

Facing pressure from thousands of chanting teachers, Republican lawmakers voted to override the governor. Bevin was not happy about that, which led to another mistake. He began hurling insults at teachers.

During the April 2018 strike, he described teachers as “selfish.” He called them “thugs.” He even went so far as to blame teachers for child abuse during the work stoppage.

“You know how many hundreds of thousands of children were left home alone today? I guarantee you somewhere in Kentucky today a child was sexually assaulted that was left at home because there was nobody there to watch them,” Bevin told reporters during the April strike. “I guarantee you somewhere today, a child was physically harmed or ingested poison because they were left alone because a single parent didn’t have any money to take care of them.”

Bevin’s comments sparked a fierce backlash from teachers and lawmakers from both parties. One of those critics was from the state’s Democratic Attorney General at the time, Beshear.

1/3 “Gov. Bevin’s comments last night saying teachers rallying in Frankfort led to children being sexually abused are morally reprehensible and must be condemned by all Kentuckians. — KY Attorney General (@kyoag) April 14, 2018

Republicans and Democrats in the House went as far as to pass their own resolutions publicly denouncing Bevin’s comments. Meanwhile, teachers warned Bevin that they would “Remember in November,” when he was up for reelection. And they did.

Education was central to Beshear’s campaign

Beshear made a smart move when he announced his run for governor earlier this year: He made education a central part of his campaign for governor. His platform on education for the state gives every teacher an immediate $2,000 across-the-board pay raise and ensures teachers aren’t paid less than $40,000. He also proposed new funding streams for the pension system.

He even picked a high school assistant principal, Jacqueline Coleman, as his running mate. As Lt. Governor, Coleman would oversee the administration’s education reform agenda.

Coleman, who has a large following on social media, has been a staunch critic of Bevin’s policy proposals.

“We’re facing teacher shortages here in Kentucky, but Matt Bevin has spent his time bullying educators and tearing down public education,” she tweeted earlier this month. Unsurprisingly, Beshear and Coleman snagged the endorsement of the teachers union.

Bevin scoffed at the Democrats’ education proposals, calling them unfeasible. He had instead promised education reform that would give more students a chance to attend private schools — something public school teachers hated. He also promised to allocate money from the state’s budget surplus to the teachers’ pension system.

By the summer of 2018, Bevin’s relationship with teachers was already ruined. Teachers organized against his campaign the way they organized their walkouts: through social media.

The Kentucky 120 United group posted volunteer opportunities for Beshear’s campaign on its Facebook page and repeatedly encouraged members to participate. That’s also where teachers coordinated get-out-the-vote events at dozens of elementary schools. And it’s where they griped about Bevin to each other in private.

So teachers may not be solely responsible for Beshear’s historic win, but they sure opened the governor’s mansion door for him.

Correction: A previous version of this article incorrectly reported the last time Kentucky had a Democratic governor. It was in 2015.