improve error handling when file finishes early