Moved end iterator assignment out of for loops.