faster history algorithm