Added more safety to searching in case of EOF error