finished adapting the script to use JSON and cleaned up the code