generate_json speed improvements, code cleanup, comments