After a horrific two-year ordeal across three countries - being bought and sold by people traffickers and surviving running out of fuel on an inflatable boat while trying to cross the Mediterranean - Mohamed finally gave up hope.
The Somali man's wife Leyla, 21, recalls the day he burnt himself to death after hearing that they were not on a UN refugee list.
They were to be evacuated from the Triq al-Sikka Detention Centre, run by a pro-government militia in the capital, Tripoli, where the UN provides humanitarian assistance. (The names of the couple have been changed for Leyla's security.)
"We were told it would be our turn to leave next. So when the new list came out, Mohamed asked me to go and check it. But our names weren't there. I had to tell him that we'd been passed over again.
"So many people who came in after us had already left. Mohamed was so upset and confused. It was just before early evening prayers when it happened. I saw Mohamed. He was burnt. I was told he had covered himself in petrol and set himself on fire."
'Scheduled for evacuation'
Leyla says that conditions at the detention centre, one of about 12 in western Libya nominally run by the government in Tripoli, were deplorable.
"I was locked in a small room with 50 other women and just a bucket for a toilet.
"We barely ate and there wasn't enough water. So many people were sick with TB, some died in my arms. I was beaten up and we were tortured - with electricity," she told the BBC.
Mohamed died later in hospital, she said.
The UN refugee agency (UNHCR) told the BBC UN staff had visited the detention centre on the day he died, but that his death was unrelated to the visit.
"The deceased asylum seeker and his wife were both scheduled for an evacuation the next month," it added.
The UNHCR denies that its work at the centre legitimises it, and says that it actually helps to improve conditions there. But some aid workers disagree and two groups have stopped working with the UNHCR.
The couple fled Islamist al-Shabab militants in Somalia in 2016 and ended up in the hands of people-trafficking gangs in Libya.
After a shootout between smugglers in Bani Walid, dubbed the trafficking capital of western Libya, they escaped and managed to board a blow-up boat with 140 other migrants to attempt the crossing over the Mediterranean.
But the boat ran out of fuel and Libyan coastguards, who are trained and backed by the European Union (EU), forcibly took them to Tripoli, where the migrants were met by UN staff and then transported by military trucks to the detention centre.
Libyan coastguard 'collusion'
The UNHCR, whose mandate is to protect refugees, and its sister agency the UN's International Organization for Migration (IOM), also face criticism from other humanitarian workers and migrants for co-operating with the Libyan coastguard, which has been accused of human rights violations during some sea rescues.
Since 2015 both the UNHCR and IOM have been providing training workshops and equipment for the coastguard, which intercepted more than 15,000 people and returned them to Libya last year.
Some say this co-operation with the coastguard compromises the impartiality of the two UN agencies.
"They work together with the EU to ensure that the migration problem is not coming to Europe," a humanitarian worker said.
"This is the aim of the EU and some of the European states, and I think they [UNHCR and IOM] are the implementers of that on the ground."
About 800,000 migrants and 50,000 registered refugees and asylum-seekers are currently living in Libya. The European Union has tightened measures to prevent migrants crossing the Mediterranean.
In the first three months of 2019, some 15,900 refugees and migrants arrived in Europe via Mediterranean routes - a 17% decrease on the same period in 2018.
In a statement, the EU says that it "does not seek to stop global migration, but works with international partners to manage international migration, protect migrants' human rights, prevent perilous irregular journeys exploited by illegal human traffickers and ensure opportunities for legal and safe pathways".
"Global migration has to be addressed via numerous channels and in a multilateral approach. In the case of Libya, the EU works with partners present on the ground, including UN agencies such as UNHCR and the IOM," the statement adds.
But for Julien Raikman, the MSF's mission head in Libya, migrants should not be taken back to Libya: "It is totally illegal actually and this is what we don't understand. We say that this is not a port of safety."
More on Libya and the migrant crisis:
The UNHCR and IOM say they are acting in the interests of saving migrants' lives.
"We acted in the migrants' interests, not the coastguard's. We advocate forcefully that migrants returned to Libyan shores by the Libyan coastguard should not be detained arbitrarily and indeed that they should not be detained at all. Day in day out our staff enter these detention centres seeking to assist migrants," the IOM told the BBC.
The UNHCR says its presence at ports where the Libyan coastguards bring intercepted migrants is important because it can register them and provide assistance.
"I don't think that being at disembarkation points is participating in criminal mechanisms," UNHCR country manager Jean-Paul Cavalieri, told the BBC.
But critics, including former UNHCR employees, feel their presence legitimises the unlawful return of migrants to Libya and their transfer to detention centres.
"Refugees like Leyla risk being tortured, abused by guards arriving high on drugs, drunk at night, abusing, beating up refugees in total impunity - deciding that they are going to intentionally starve them for weeks," said Giulia Tranchina, a lawyer who has represented people trapped in Libya.
The detention centres are said to be more like prisons and an opportunity for militias to make money.
The UNHCR's top official admits: "These detention centres, at least some of them, they work on a business model that involves smugglers, traffickers, sometimes forced labour."
He argued that the UNHCR's work at the detention centres did not legitimise them - as they would exist anyway - and by registering jailed migrants it prevented migrants from being sold on to traffickers.
However, witnesses told the BBC many detainees never manage to get registered.
The UN and other non-governmental organisations find that Libya is a difficult environment to work in - and the UNHCR is not even recognised by the government in Tripoli.
Since Muammar Gaddafi was ousted and killed in 2011, the country has become a battleground, with rivals militias fighting for control. The UN-backed government in Tripoli has little power itself, instead relying on various armed groups to fight off a rival authority based in the east, led by the rogue general Khalifa Haftar.
A tranche of confidential documents, unpublished reports and email correspondence given to the BBC by several sources also reveals a chaotic and "dysfunctional" humanitarian response, especially when dealing with other NGOs.
"I have never seen this level of incompetence," a humanitarian worker in Libya, who has worked closely with UNHCR, said.
Since 2014 the EU has spent 338m euro ($377m; £303m) on projects in Libya, which it says is to help migrants like Leyla and Mohamed, through funding the UN and NGOs.
A UN audit of the UNHCR published in March revealed mismanagement of funds, and multiple instances of failures to assess how much aid was needed and failures in verifying its delivery.
The report found $2.9m had been overspent on aid which was not then used. It also suggested bidding for contracts was not transparent and questioned why deals were done in US dollars rather than local currency.
Documented evidence given to the BBC shows that donors have been made aware of failings since 2018.
In June, an email leaked to the BBC, addressed to EU officials and diplomats, spoke of deteriorating conditions in the detention centre in Khoms, including human rights abuses, disappearances of migrants and suspected collusion between prison guards and human traffickers.
More on EU migration crisis:
Two foreign NGOs, including International Medical Corps, then unilaterally suspended their work there, citing a lack of response by both the UNHCR and IOM.
In December, the UNHCR paid for a controversial "Gathering and Departure Facility" in Tripoli at a cost of $3.5m, much trumpeted by the EU as an "alternative to detention" free from the abuse seen at other detention centres.
But even access to the compound is controlled by Tripoli's Ministry of Interior and there is no freedom of movement for the migrants waiting there to be evacuated to neighbouring Niger.
Leyla has now left Triq al-Sikka Detention Centre. She said: "Someone from the Libyan army told me that Mohamed's death was ordained by God and that I should accept it and let it go.
"They kept me there, in the same prison for three months after he died."
Traumatised by her ordeal, she is now living in a tent in a UNHCR transit camp in Niger.
She says returning to Somalia would be "a death sentence", but she remains frightened and still has no idea about her fate.