Clean up json serialization code a bit and add debug logs