Adding JSON encoding, cleaning up bad stuff