Added some logging and error handling