Remove redundant code and use C++ range fors
[C++11] Use more range based fors