Ahamed Aathil Mohamed Samsudeen was under police surveillance when he attacked a group of people at an Auckland grocery on Friday. Authorities knew he was a threat, but New Zealand’s laws could not keep him in jail.

They were aware that he previously possessed knives and extremist materials, and that he sympathized with the Islamic State. He had spent three years in prison but was released in July.

Police had been working around-the-clock, watching his every move for weeks. Prime Minister Jacinda Ardern had been briefed on his case.

That the 32-year-old immigrant was still able to grab a kitchen knife from a display at the store and begin a stabbing rampage that injured seven people has raised questions about New Zealand’s counterterrorism laws.

On Saturday, Ardern committed to tightening those laws by the end of the month. “We must be willing to make the changes that we know may not necessarily have changed history, but could change the future,” she said at a news conference.

Undercover officers were outside the store on Friday, Police Commissioner Andrew Coster said. Those officers, including members of the armed special tactics group, heard shouting and saw shoppers running from the store. They rushed in and “engaged” Samsudeen, fatally shooting him.

A Tamil Muslim from Sri Lanka, Samsudeen arrived in New Zealand in 2011 on a student visa and sought refugee status. He first came to police attention in 2016 after he expressed sympathy on Facebook for recent terrorist attacks, violent war-related videos and comments advocating violent extremism. The police spoke with him twice.

In 2017, he was arrested at the airport in Auckland; authorities believed he was headed to Syria, where the Islamic State was involved in that nation’s civil war. A search of his apartment turned up a hunting knife and “restricted publications,” Ardern said.

He was released on bail, but he was arrested again in 2018 after he bought another knife. In another apartment search, police found extremist materials, including Islamic State videos. This time, he was kept in custody, spending three years in prison after pleading guilty to numerous charges.

In May, he faced new charges in connection with those videos. A jury found him guilty, but because the videos didn’t contain violence, such as killings, that other Islamic State videos did, they weren’t classified as the worst kind of illicit material, the Associated Press reported. High Court Judge Sally Fitzgerald released him on a year’s supervision at an Auckland mosque after a leader pledged to assist him.

In 2018, ministers were briefed on Samsudeen’s case and directed officials to amend counterterrorism legislation, including to criminalize preparatory activities related to terrorist plans. But as time passed, officials had to “prepare for the potential that we may run out of legal avenues to detain him,” Ardern said.

Ardern was briefed again on his case in May. She said she looked into whether prevention measures could be used against Samsudeen — but was advised that it was not possible. A draft bill, which would allow earlier intervention, was introduced in parliament in April.

Meanwhile, Samsudeen was released in July with conditions including a full-time surveillance team and also technological restrictions. But this did not mean police could literally stand right on top of him.

“Long-term surveillance of a surveillance-conscious subject is very difficult,” Coster, the police commissioner, said at Saturday’s news briefing. “Surveillance teams must work hard not to be recognized otherwise the surveillance become ineffective.”

Coster differentiated between “surveillance” and “a security detail,” saying he had no legal grounds to detain Samsudeen.

In late August, officials raised the possibility of expediting the amendments to the counterterrorism legislation. Within 48 hours of these discussions, the justice minister contacted the chair of the select committee, Ardern said, with the intention to speed up the law change.

“That was yesterday, the same day the attack happened,” Ardern continued. “As you can see, agencies used every tool available to them to protect innocent people from this individual. Every legal avenue was tried.”

It was around 2:27 p.m. when Samsudeen entered the grocery store after traveling by train. The surveilling police followed him from a distance along the way. He shopped around with a grocery cart for about 10 minutes before he grabbed the knife and began the stabbing attack.

Three of his seven victims remained hospitalized Saturday in critical condition.

“I know we are all continuing to think of them and the traumatic experience they have been through,” Ardern said. She ended with a quote from the imam of Al Noor Mosque in Christchurch: “All terrorists are the same, regardless of their ideology. They stand for hate. We stand for peace and love.”