Added some error handling code