Justin Theroux, left, and Emily Blunt appear in a scene from, “The Girl on the Train.” (Barry Wetcher/Universal Pictures via AP)

(Obviously, this post includes many spoilers about “The Girl on the Train” movie and book.)

There are many troubling questions in “The Girl on the Train,” the international best-selling thriller that was just adapted as a movie, but two big mysteries stand out: Who killed Megan (Haley Bennett)? And what happened during the night that Rachel (Emily Blunt) can’t remember?

As with any twisted book, the ending is a fraught topic: Some love it, some hate it. For better or worse, “The Girl on the Train” film keeps the ending mostly the same as in the book, save for a couple scenes. And there’s one new, key character who helps reveal why the questions are connected.

The important ingredient in both mysteries, obviously, is that Rachel’s ex-husband, Tom (Justin Theroux), is a total psychopath. When they were married, Rachel was struggling to come to terms with her infertility; it led to her severe depression and a drinking problem. While Tom always told her that she did horrible things when she blacked out drunk — and that’s why he left her for his mistress, Anna (Rebecca Ferguson) — it turns out that Tom was gaslighting her the entire time. He was actually the violent abuser.

Rebecca Ferguson (Anna), left, and Justin Theroux (Tom) appear in The Girl on the Train.” (Barry Wetcher/Universal Pictures via AP)

Tom also told Rachel that he got fired because she humiliated him at his boss’s barbecue when she blacked out; and of course, Rachel can’t remember what happened. Director Tate Taylor explained that he wanted Rachel to have a visual “lightbulb” moment when she realized that Tom was a liar, so he invented the character of Martha (Lisa Kudrow), Tom’s boss’s wife, who runs into Rachel on the train. When Rachel spots her late in the movie, she approaches Martha and apologizes for her behavior at the barbecue.

Martha is visibly confused. She says all that happened during the barbecue is that Rachel didn’t feel well; and then Tom got so angry that he physically threw her out of the barbecue and started screaming at her. Oh, and Tom was fired because he slept with every woman in the office. “We all felt so bad for you. He was such a bad guy,” Martha says.

“Martha” is a pretty solid storytelling technique — it helps to have another person confirm to Rachel that Tom is evil. From there, Rachel suddenly flashes back to the night she couldn’t remember (the same night Megan went missing), and realizes she confronted Tom, who hit her so hard that she was knocked unconscious. She puts 2 and 2 together, and remembers she saw Megan get in Tom’s car that night — and Tom must have been the one that killed her.

Later, Rachel rushes to Anna’s house, even though Anna is Rachel’s ex-husband’s mistress — because Anna should know the truth about Tom. However, Anna has already found out through her own detective work that Tom was sleeping with Megan before she died. While Rachel frantically tries to explain that Anna and her baby have to escape, Anna refuses. “I know he’s cheating,” she says. “I’m not leaving him.”

Her attitude is infuriating, until Tom gets home. Rachel starts screaming that he killed Megan, and she remembers everything from that mysterious night, including the fact that Megan got in his car. This seems to wake Anna up: She confronts Tom about sleeping with Megan, who worked as their nanny. Tom admits it and doesn’t feel bad: “You were so tired all the time.”

Megan (Haley Bennett) and Tom (Justin Theroux) in DreamWorks Pictures’ “The Girl on the Train,.” (Barry Wetcher/DreamWorks Pictures/Universal Pictures)

The audience sees a flashback of how Megan died: When she confronts Tom, saying that she was pregnant and he was probably the father, he flippantly tells her to get an abortion. He says lots of other horrible, gasp-inducing things, like “You’re a s—– nanny. What makes you think you’re going to be a good mother?” Megan tells him that she’s keeping the baby, and then in the insult that sends him over the edge, tells him that without Rachel, Anna or her, “you’re just a pathetic, impotent man.”

Tom loses it, of course, and proceeds to violently bash Megan’s head in, killing her. When viewers flash back to present day, Tom is trying to strangle Rachel. She escapes and runs outside, where she manages to stab him in the neck with a corkscrew — where the audience sees everything, including all the blood. When Anna comes outside, she takes one look at the situation . . . and the screws it into his neck even tighter, really making sure he’s dead.

So even though they were once enemies, Rachel and Anna work to kill the man who hurt them both, and back each other up when the cops come to investigate. In the book, it was a difficult thing to imagine given how the two women despised each other. On screen, seeing how disgusting Tom is to them both, it almost (almost) makes sense.

Read more:

REVIEW | ‘The Girl on the Train’: Even Emily Blunt can’t enliven fatally dreary whodunit.

What do we think of the casting for ‘The Girl on the Train’?

‘The Girl on the Train’ and five other bad movie commutes