Added more error handling code