Refactor to improve error handling and code clarity

Refactored error handling in some JSON functions; added an error parameter.

Moved some memory allocation of strings to the stack.

Removed some unused functions.