replace naive string concatenation by use of cStringIO which is much faster