add json support, clean up code