CHICAGO (AP) — A man who fatally shot his ex-fiancee outside a Chicago hospital before killing two people inside the building was once kicked out of the city’s firefighting academy after threatening a female cadet, officials said Tuesday.

Juan Lopez, who died following the shooting Monday at Mercy Hospital, was also the subject of a protection order request filed four years ago, and he legally purchased several guns in recent years, police said.

It was unclear whether Lopez shot himself or was fatally shot by police.

Four years ago, fire department officials learned of the threats to the cadet and told Lopez that he would be disciplined. He was dismissed after he went AWOL, fire department spokesman Larry Merritt said.

Merritt did not have any details of the past threats. But they were made the same year a woman sought an order of protection against Lopez because he was incessantly texting her. Police said they have not determined if the woman was granted an order of protection. Lopez was not criminally charged.

On Monday, Lopez’s first victim was Dr. Tamara O’Neal, to whom he had been engaged. O’Neal had recently called off their engagement, and Lopez confronted her about returning the engagement ring, police said.

After shooting the emergency room doctor near a hospital parking lot, the gunman ran into the medical center, where he continued firing. The gunshots killed a police officer and a pharmacy resident, authorities said.

Lopez had a permit to possess a concealed firearm, but it was unclear if officials knew about the 2014 complaint when the permit was granted, Chicago police spokesman Anthony Guglielmi said.

Lopez had legally purchased four guns in the last five years and worked for the Chicago Housing Authority, Guglielmi said.

Investigators identified the other shooting victims as Dayna Less, 25, who worked in the hospital’s pharmacy and had recently graduated from Purdue University, and Officer Samuel Jimenez, 28, who joined the department in February 2017 and had recently completed his probationary period. Police said he was a married father of three children.

“This officer, all of those officers, are heroes. They saved a lot of lives because we just don’t know how much damage he was prepared to do,” Chicago Police Superintendent Eddie Johnson said late Monday outside another hospital, just minutes after leaving the slain officer’s family.

Mayor Rahm Emanuel, fighting back tears, said the victims were “all going about their day, all doing what they loved.” He added: “This just tears at the soul of our city. It is the face and a consequence of evil.”

Lopez, 32, and O’Neal had been arguing in the hospital parking lot. When one of O’Neal’s friends tried to intervene, “the offender lifted up his shirt and displayed a handgun,” Johnson said.

The friend ran into the hospital to call for help, and the gunfire began seconds later. After O’Neal fell to the ground, Lopez “stood over her and shot her three more times,” a witness named James Gray told reporters.

When officers arrived, the suspect fired at their squad car then ran inside the hospital. The police gave chase.

Inside the medical center, Lopez exchanged fire with officers and “shot a poor woman who just came off the elevator” before he was killed, Johnson said, referring to Less.

Jennifer Eldridge was working in a hospital pharmacy when she heard three or four shots that seemed to come from outside. Within seconds, she barricaded the door, as called for in the building’s active shooter drills. Then there were six or seven more shots that sounded much closer, just outside the door.

“I could tell he was now inside the lobby. There was screaming,” she recalled.

The door jiggled, which Eldridge believed was the shooter trying to get in. Some 15 minutes later, she estimated, a SWAT team officer knocked at the door, came inside and led her away. She looked down and saw blood on the floor but no bodies.

“It may have been 15 minutes, but it seemed like an eternity,” she said.

Maria Correa hid under a desk, clutching her 4-month-old son Angel, while the violence unfolded. Correa was in the waiting area of the hospital for her mother-in-law’s doctor appointment when a hospital employee told them to lock themselves in offices.

She lost track of how many shots she heard while under the desk trying to protect her son for 10 to 15 minutes.

“They were the worst minutes of our lives,” Correa said.

The death of Jimenez comes nine months after another member of the Chicago Police Department, Cmdr. Paul Bauer, was fatally shot while pursuing a suspect in the Loop business district.

Mercy has a rich history as the city’s first chartered hospital. It began in 1852, when the Sisters of Mercy religious group converted a rooming house. During the Civil War, the hospital treated both Union soldiers and Confederate prisoners of war, according to its website.