Fix history overwriting after max length.