Error handling fixed (not tested yet), JNI code refactored much