Misc fixes and improvements

Hi,

got a few fixes which have been running in production since december (and parts of those since earlier);
one generic fix, and a few Java-compat improvements (such as capabilities support). Please see the commits for details.

Thanks!