A violent five-day prison uprising in Brazil in which nine inmates have been killed appears to have come to an end.
The revolt began on Sunday after a clash between rival gangs
About 170 prison visitors being held in the revolt, which began on Sunday in the Urso Branco prison in the Amazon state of Rondonia, are to be freed.
The authorities confirmed that they had accepted most of the demands made by the prisoners to end the stand-off.
Inmates were protesting against overcrowding and called for the resignation of the prison directors.
They were also demanding the right to use mobile phones.
The state security secretary confirmed that the prison governor would be replaced and, as requested, up to 30 inmates would be transferred elsewhere.
Negotiations had appeared deadlocked earlier in the day when a security official told AP news agency that the authorities could only agree to 17 of 20 demands made by the prisoners.
Police had surrounded the prison and state Governor Ivo Cassol said he would send police in to retake the compound as a "final measure".
The rebellion was reportedly sparked by a clash between rival gangs.
It will be remembered as one of the bloodier uprisings of recent times, says the BBC's Steve Kingstone in Rio de Janeiro.
Inmates began a series of revenge killings in which at least one victim's head was cut off and thrown to the ground from the prison roof.
Another man was hacked to death and body parts were thrown out of the prison.
At least nine prisoners are known to have died in the fighting.
Three other men may also have been killed according to a group of inmates who clambered over the prison wall on Thursday.
As is often the case in Brazil, the revolt was about overcrowding, our correspondent says.
Designed to hold 360 people, the prison houses more than 1,000 inmates.
State officials say this gruesome sequence of events should act as a warning that the country's penal system needs to be reviewed urgently.