Improve history robustness against corrupt files

Fixes #1581