modified file reading algorithm