Refactor historyItems(...) function.