fix($mdUtil): fix bugs in iterator's `next()`/`previous()` methods

(Since `next()` and `previous()` are almost identical, the code was
slightly refactored for DRY-ness.)