Added logging and additional logic to handle uncaught exceptions