changed while loop to for loop with range
refactored filename and datafiles loop