Address various problems with source code as suggested by @demobox and @nacx.

Fixups in code, mock and live tests.