Fix up I/O and simplify the json code. General clean up.