Refactor reading and processing of stdout of remote commands