JNI Wrapper: Improved error handling + improved handle management code.