Adding JSON error handling and fixing location headers.