Refactor, and update JSON encoding and decoding routine.