Uses const_iterator instead of iterator where ever applicable
Extracts common cleanup into a function