using cStringIO for performance