Improve efficiency of ReplayIterator; fix a bug