Fixes #857, #392 - also closes #700

@macdonst - any chance of a review on this? My java is teh sad.