Australian MPs have passed a landmark bill with an opposition amendment making it easier for sick refugees held offshore to be treated in the country.
This is the first time in decades a government has lost a vote on its own legislation in the lower house.
The move is a blow for PM Scott Morrison's minority government's highly controversial immigration policy.
Since 2013, Australia has sent asylum seekers arriving by boat to detention centres on Nauru and Papua New Guinea.
Critics say it has harmed the welfare of detainees, including children.
Doctors have long warned of inadequate medical facilities on the islands, while the UN has previously described the camp conditions as "inhumane".However, Mr Morrison said: "There is no form of this bill that does not weaken our border protection."
Australia has long defended its offshore detention policy by arguing that it stops deaths at sea and disrupts the trade of people smuggling.
The bill passed in the House of Representatives by one vote after the Labour opposition and crossbench MPs agreed on last-minute amendments.
It is expected to sail through the upper Senate later this week where it will become law.
Why does this matter?
It's hugely significant that a government has lost a key parliamentary vote in its lower house - this hasn't happened in almost 80 years according to the Australian Broadcasting Corporation and comes ahead of a federal election later this year.
In 1941, then Prime Minister Arthur Fadden immediately resigned after he lost a budget vote in the lower house.
Tuesday's defeat comes as a blow to Mr Morrison, Australia's sixth prime minister in six years, and raises questions about whether he can remain in office. The government lost its majority late last year after losing a by-election.
But Mr Morrison has ruled out a snap election, saying last week that he wouldn't "be going off to the polls" even if he lost the "stupid" bill.
"Votes will come and go, they do not trouble me," he said on Tuesday after the government's defeat. "The Australian people can always trust us to... ensure the integrity of our border protection framework."
Mr Morrison's coalition government has to call an election by May.
So what does the bill allow?
Doctors will now have the power to recommended transfers for refugees on Nauru and Manus to Australia for treatment.
However, the immigration minister could ask an independent panel to review the medical assessment, and would have some authority to overrule it.
Previously, doctors had reported that their medical transfer recommendation were ignored by authorities.
Refugee lawyers thus had to apply for court orders to bring ill people to Australia. There were 44 medical transfers achieved through court battles.
Why was there a push for this law?
Last year, Australians were horrified by reports of a mental health crisis among children in detention. Doctors reported affected children too depressed to eat or sleep, and attempts of suicide among those as young as 11.
The wave of public backlash pushed the government to evacuate more than 100 children and their families from Nauru to Australia.
Advocates warned that a similar mental health crisis, and a plague of other medical issues, was also constant among the 1,000 adult detainees stuck on Nauru and Manus Island.
In one of several high-profile cases, an inquest in July found that the death of Iranian refugee Hamid Khazei on Manus Island from a foot infection could have been prevented if he had been transferred to Australia earlier for medical treatment.
Hamid Khazei died after Australian authorities delayed his medical treatment
How many sick asylum seekers are there?
The government had warned that the bill provided a "loophole" that would allow all of the remaining asylum seekers to reach Australia.
However, it declined to answer whether that meant all offshore detainees were seriously ill.
Medecins Sans Frontieres, which provided mental health treatment to Nauru detainees last year, has noted that depression and suicide ideation was widespread and directly caused by a policy of indefinite detention.
"Five years of indefinite limbo has led to a radical deterioration of their mental health and wellbeing," said MSF Australia's director, Paul McPhun.
In a public letter to MPs this week, Doctors4Refugees, an advocacy group of physicians, also identified dozens of sick refugee cases who had received inadequate treatment.
These included "life-threatening" heart conditions, kidney stones, tuberculosis and diseases common in the sub-tropical environment, such as malaria, dengue fever and chronic fungal infections.
Refugees on the islands had told the BBC they were pinning their hopes on the vote.