A witch's brew of unemployment, inequality and poverty, mixed with South Africa's violent past, are to blame for attacks on foreigners that so tarnished the country's image last month, experts say.
The deadly assaults rocked South Africa's relations with its neighbours but especially with Nigeria, whose president, Muhammadu Buhari, begins a state visit here on Thursday.
At least 10 South Africans and two foreigners were killed after mobs descended on foreign-owned stores in poor districts in and around Johannesburg.
Analysts told AFP that the violence -- the latest in a rash of such attacks over the past decade -- is mainly rooted in a sickly economy and faltering politics, stirring rivalry for jobs, especially in manual labour.
South Africa is a magnet for poor migrants from neighbouring Mozambique, Lesotho and Zimbabwe, but even further afield, including Nigeria and even South Asia.
"South Africa has a terrible combination of extremely high unemployment... and the highest inequality rate in the world," said Nicolas Pons-Vignon, economic researcher at Johannesburg's Wits University.
Competition for jobs, social services and housing "create a fertile terrain for mobilisation along identity lines," he said.
Reliable figures are sketchy, but the last census in 2011 counted just over 2.1 million "international migrants", around four percent of South Africa's population at the time.
Joblessness hit a record 29 percent this year, reaching above 50 percent for youth.
Loren Landau, a researcher for the African Centre for Migration & Society, said the country's politicians were also indirectly to blame for stoking the mood.
"It's anti-immigrant but it's not an immigrant issue," Landau told AFP.
Rhetoric tinged with xenophobia ran high in the runup to elections this year. Both the ruling African National Congress (ANC) and rival Democratic Alliance (DA) pledged to crack down on irregular migrants.
Politicians are failing to create jobs, and "when you don't have things to offer, you turn to blaming others," Landau said.
Human rights lawyer Sharon Ekambaram pointed out that most tensions played out in densely populated, poorly serviced townships.
"We cannot understand the xenophobia of today without locating it in deep, deep poverty" and the government's failure to "transform society" after apartheid, she told AFP.
Another factor is South Africa's own troubled past, whose trauma is felt today, said Verne Harris, head of the Nelson Mandela Foundation.
"In 1994 we inherited a deeply wounded society," Harris said.
"Old patterns of power, property and wealth haven't been fundamentally transformed. That translates into deep-seated anger and high levels of violence."
That brutality is also a legacy of the anti-apartheid struggle.
"Townships were deeply politicised and organised by groups that used violence as part of their anti-apartheid campaign," said Landau.
Those people remained and "didn't give up their violent ways."
But researcher Savo Heleta also noted the irony of xenophobia in a country that was helped by other African states during the struggle.
Many gave the ANC arms, money and political support, allowing it to topple the regime and win every election since.
The liberation movement was an illustration of "African unity", said Seleta, who works for the Nelson Mandela University in Port Elizabeth.
"There is a failure to speak about this solidarity."
'Failure of the state'
Successive governments have since failed to fill the gaps in social provision created by white supremacy.
"In many of our communities, failures of the state have led people to rely on informal systems of power," said Harris.
With rampant crime levels and a thin police force, those who cannot afford to live in privately secured areas are confronted with thugs and mob justice.
Gang leaders use the hardship of townships to stoke anti-foreigner sentiment and "reinforce their authority at the local level," said Landau.
"And they get away with it," he added.
Amnesty International has condemned the government's failure to prosecute suspected perpetrators of xenophobic crimes.
The attacks are "a direct consequence of years of impunity and failures in the criminal justice system," it said in a statement earlier this month.
Former president Jacob Zuma has been charged with 16 counts of corruption during his time in office. The accusations prompted his resignation last year, although prosecution has been sluggish.
"South Africa is a very bad example of people doing terrible things and getting a get-out-of-jail-free card," said Heleta.
"When no one gets arrested, that's when people start to realise that they can do terrible things."