shell: Refactor output and error handling to be more flexible