added a check for the end of file