Improved usage of exit codes in CLI commands

Includes some light cleaning in api operations