refines the algorithm to rename the files