A retired Massachusetts detective fatally shot his wife—then staged her murder as a suicide—so he could be with another woman and keep his pension all for himself, prosecutors alleged in court Thursday.

Brian Fanion, a 55-year-old former Westfield Police detective, pleaded not guilty in Hampden Superior Court Thursday morning to a first-degree murder charge in connection with the May 2018 death of his wife, Amy. Authorities at the time ruled the 51-year-old’s shooting death a suicide.

Prosecutors alleged Fanion, who joined the force in 1985 and worked as an evidence room detective, was having an affair with another woman at the time of his retirement last year and was desperate to avoid “splitting” his future pension with Amy.

“There was only one way to enjoy his retirement and his new love affair and that was to murder his wife,” Assistant District Attorney Mary Sandstrom said in court.

The intimate affair began last March, prosecutors alleged, which prompted Fanion to look into what effect divorcing his wife of 35 years and living with his new girlfriend would have on his pension.

Fanion’s defense attorneys dismissed the affair in court Thursday and argued Amy had long struggled with severe anxiety.

“He did have a female friend, it was a non-sexual relationship, they communicated primarily by text message,” defense attorney Jeffrey Brown said in court Thursday, claiming that the friend was also close to Amy.

On May 2, 2018, Fanion was allegedly home on a lunch break when he and his wife got into an argument about “retirement plans.” Dispatchers arrived to the Fanion house a little after noon, prosecutors said, after receiving reports of a shooting and found the former detective “standing beside his wife” inside their dining room and “holding her hands.”

Authorities said the 51-year-old sustained a fatal gunshot wound to the right side of her head—which prosecutors said is not consistent with being self inflicted. They say the wound is from a gun held about 18 inches away from her head.

“She had no suicidal ideations, no issue of self harm. This was a woman who was happy, who expressed her love for Brian Fanion, who wanted to continue living,” Sandstrom said. “This was a woman who was not depressed, had no mental health disorder and very much someone who was not suicidal or prepared to take her own life.”

Fanion admitted to police he brought home his department-issued handgun that day, despite his wife’s anxiety over firearms. He initially told authorities that while he was in the bathroom, he “heard a gunshot” and came out to “find his wife on the floor,” prosecutors said.

“While on the scene, that story changed, and he said that he was inside the bathroom and when he went outside the bathroom he saw his wife put the gun up to her head and discharge the bullet,” Sandstrom said in court while Fanion stood a few feet away, frowning and rocking slightly back and forth.

Prosecutors also cited several internet searches on Fanion’s computer at the Westfield Police Department that pointed to his alleged involvement, including his attempts to look up information on the effects of divorces on pensions, gunshot residue, and household poisons people can easily overdose on.

“Mere hours before the murder he was looking for apartments and places to stay,” Sandstrom said. “This was a murder in planning for quite some time.”

Defense attorney Jeffrey Brown slammed the prosecution’s allegations, claiming that Amy Fanion had long struggled with anxiety and depression, and was somebody “not without her demons”—which she chronicled daily in a diary. Brown also noted that she’d stopped taking several medications before her death.

“Her diary is replete with issues she was having and seeking God’s help to get her through these issues,” Brown said, stating that prosecutors are “not interested in the truth here.”

Brown also read a statement from the 51-year-old’s immediate and extended family expressing their support for the former detective, who joined the force at age 15, stating they “unequivocally” believe Amy’s death was self-inflicted.