replaced while loop with fread