factor out and cleanup iobuf