Do you live in the real world? Prisons aren't safe because they are full of violent offenders! It's pretty obvious that when you densely pack the bottom tier of society into small spaces that the outcome will be a whole load of violence - of all forms, rape (which is about power and control), gangs (which is core to the human species), fights and murder.
#1 on the list would be to stop sending so many people to prison, one way or another. Then you can afford to spend more resources per capita to better rehabilitate/reform/etc. the smaller number of inmates that remain.