Merged from master. Also moved building of command_stream_t into its own function.