BELFAST, Maine — The former tax examiner accused of killing a well-loved Belfast woman last summer pleaded guilty to murder Monday morning and will spend 50 years in prison, according to a deal worked out with state prosecutors and his defense attorney.

Todd Gilday, 44, will be sentenced on Aug. 20 for the slaying of Lynn Arsenault, also known as Lynn Day, according to officials at Waldo County Superior Court.

Last fall, he had pleaded not guilty by reason of insanity to the charges of intentional or knowing murder, aggravated attempted murder and elevated aggravated assault. However, Gilday indicated last week he would change his plea.

He was brought to Belfast from Two Bridges Regional Jail in Wiscasset for the hearing, presided over by Justice Robert Murray, and will remain at that jail without bail until his sentencing. He is represented by attorney Jeremy Pratt, and Assistant Attorney General Leane Zainea is prosecuting the case for the state of Maine.

According to police investigators, Gilday was upset over a friend’s child custody case and had consumed a large amount of opiates the night of Aug. 28, 2013, when he went to Mathew Day’s Waldo Avenue home with a shotgun. He shot through the door, according to an affidavit filed in December by a Maine State Police detective, and once inside shot the 22-year-old Day in the stomach and arm, seriously injuring him.

Day told police that Arsenault, his 55-year-old mother, came out of a bedroom where she had been sleeping and may have tried to grab the gun from Gilday before he fired at her, shooting her in the chest.

Arsenault died of a single shotgun wound to the upper left arm and chest. The woman, who lived in both Garland and Belfast, was remembered by community members as a kind and compassionate person who would do anything to help others.

After the shootings, Gilday drove to Pen Bay Medical Center in Rockport, where he asked to be admitted to the psychiatric and addiction recovery unit at 1 a.m. He told the staff there he was a drug addict and had had a “very bad night,” according to the affidavit filed by Maine State Police Detective Dean Jackson, who said that Gilday had refused to talk to police about the case.

Day told the detective he had known Gilday for about three months and had met him through his girlfriend. Day said that he and Gilday would get drugs for each other, according to the affidavit.