Added JSON procedure and cleaned up code