remove range based loop (C++11)

Including a bit of clean-up of the code (no functional changes).