Refactor of #7887

Closes #7887

Thanks @rhatdan for most of the work

@rhatdan I changed behavior for `CmdLogin`, `CmdLogout`, `CmdPs`. Can you double check it's correct?