fix for potentially invalid iterators