Cleaned up the code for clarity and added JSON tests.
Cleaned up the code for clarity and added JSON tests.