Some improvmenets to json writing (in progress)