CP-3657 for Clearwater

Imported a bunch of Mietek's patches, and a fix for the CLI.