Refactored LinearIterator into mutable and immutable version.