add error handling and html/json output