Use C++11 auto and for range to simplify code