ROCKVILLE, Md. A Maryland woman who pleaded guilty to fatally stabbing two of her children and injuring their two older siblings in what she believed was an exorcism will go to a psychiatric hospital, not prison, a judge ruled Thursday.

Zakieya Avery of Germantown, Maryland, pleaded guilty earlier this week to two counts of first-degree murder and two counts of attempted first-degree murder, but it was up to a judge to decide whether she was criminally responsible in the deaths of her children in 2014.

Montgomery County Circuit Court Judge Terrence McGann called the children’s murders “brutal and sadistic.” But he said the testimony he heard about Avery’s behavior before the stabbings provided “strong evidence” that Avery was “delusional” when she and her housemate stabbed the children: 18-month-old Norell Harris and 2-year-old Zyana Harris, both of whom died, and their older siblings, 8-year-old Martello Harris and 5-year-old Taniya Harris.

Get Breaking News Delivered to Your Inbox

Avery, 31, and her housemate believed the children were possessed by demons. The women’s bizarre behavior before the killings included writing extensively in a shared journal about demons and imagined spirit boyfriends. The women, who believed they were “demon warriors,” at one point lived in a car for nine days in order to avoid demons in their house; they did not bathe or eat for several days during that time.

The judge noted that Avery had previously been treated for mental health issues at several facilities and had been prescribed medication but threw it away because she believed “God had delivered her from her mental illness.” Despite the fact that she had a surgical procedure to prevent pregnancy, Avery said God had reversed the procedure and she was pregnant with twins. The judge found that during the murders Avery was suffering from bipolar disorder with “psychotic features” and “borderline personality disorder.”

“I find that Miss Avery’s ritualistic, albeit barbaric, attempt to rid her children of demons is powerful evidence of the lack of rational thinking,” the judge said.

After the judge’s ruling, Avery’s attorney, Brian Shefferman, called the case “tragic” and said he believed the judge had reached the correct decision. He said it will be years before Avery might be considered for release.

Avery was in court to hear the judge read his ruling but showed no visible reaction to the decision, looking over at her attorney shortly after the judge said he found her not criminally responsible.

Avery’s cousin, Kaliha Brooks, said after the hearing that Avery was a “very loving mother” and called it “mind-boggling” that Avery killed her children.

“I can honestly say that my cousin absolutely adored and loved her children,” Brooks said.

Avery’s housemate and friend, Monifa Sanford, who participated in the killings, previously took a plea deal in the case and was also committed to a psychiatric hospital.