updated to read from the end of the file