code re-write

Re-wrote all the old code, implemented almost all of the JSON
functionality.