Added improved error handling