Changing indexed loops to C++11 rangebased for loop.
Also cleaned up a few lines of code, via the algorithms standard header.