more correct algorithm for history checking, with accompanying tests