Added logic to handle 404 & 500 errors