Remove an extra parameter and C++11 for loop-ify this code