Properly handle end of file