move common code into helper function that also takes care of the ugly alloc/free stuff.
Attach a file by drag & drop or click to upload