Bug fix interating through arrays. Move err to last parameter to better determine amount of spoace left. Minor code clean-up.