Improved exception handling and debugging