NEW YORK (AP) — A man who drove his car through crowds of people in Times Square in 2017, killing a young tourist and maiming helpless pedestrians, was cleared of responsibility Wednesday because of mental illness.

A jury in New York City accepted an insanity defense claiming Richard Rojas was so psychologically disturbed he didn’t know what he was doing.

The judge has said the finding would qualify Rojas for an open ended “involuntary mental commitment” instead of a lengthy prison term. He ordered Rojas held while he drafts an examination order, and said there would be a hearing on the matter Thursday.

Rojas, 31, was accused in an attack that injured more than 20 people and killed Alyssa Elsman, 18, of Michigan, who was visiting the popular tourist destination with her family.

The jury was instructed that if it found prosecutors had proven their case, it also had to decide whether or not he “lacked responsibility by reason of mental disease or defect.”

Rojas' attorney Enrico DeMarco called the verdict “right and humane,” adding that winning over the jury was an uphill battle “because it was such a horrible act.”

The trial, which began early last month, featured testimony from victims who suffered severe injuries from what prosecutors labeled “a horrific, depraved act.”

On the defense side, family members testified how Rojas descended into paranoia after he was kicked out of the Navy in 2014.

That Rojas was behind the wheel of the car was never in doubt. Multiple security videos showed him emerging from the vehicle after it crashed. That put the focus of the case on his mental state.

In his closing argument, prosecutor Alfred Peterson conceded that Rojas was having a psychotic episode, including hearing voices, at the time of the rampage. But Peterson argued Rojas showed he wasn’t entirely detached from reality by maneuvering his vehicle onto the sidewalk and driving with precision for three blocks, mowing down people until he crashed.

One victim’s pelvis was separated from her spine. Doctors were certain she would die, but she somehow survived. Elsman’s younger sister Eva, then 13, testified during the trial about her own injuries: broken ribs, a collapsed lung, a compound leg fracture and other wounds that kept her in the hospital for weeks.

“The defendant made a decision that day,” the prosecutor, Peterson, said. “He made a choice. … He went to the ‘crossroads of the world,’ a high profile place where everyone knows there’s lots and lots of people.”

Once there, he was “in full control of his car,” he added.

Defense lawyer Enrico DeMarco told jurors “there should be no doubt” his client met the legal standard for an insanity finding. The evidence, the lawyer said, showed Rojas “lacked a substantial capacity to know what he was doing was wrong” because of an underlying illness — schizophrenia, as diagnosed by a defense psychiatrist who testified.

DeMarco played a videotape in the courtroom of Rojas jumping out of his car after it slammed into a sidewalk stanchion. Rojas could be heard yelling, “What happened? … Oh my God, what happened?” as he was being subdued, and could be seen banging his head on the ground.

Rojas, the attorney said, “lost his mind.”