Jni fixes and minor c++ improvements

* FIX: memory leak in classes cleanup.
* some jni references were not properly cleaned up (global references are cleaned as local ref, and vice versa)
* avoid copy construction, where I stumbled upon
* minor code improvements (removed unused variables)

I know that this is a really big PR, but rebased as much as I can :)