Imporve error handling