Major re-factor, replacing manual JSON construction with a simpler approach.