Added handling of uncaught exceptions