Various improvements. Especially to make java bindings work properly.

Please merge.