Improved error handling in native code