Added JSON support, improved error handling