Added while loop to deal with sdfopen issue