Made program open file once instead of each loop