Added more error handling