rearrange blank lines to make logic clearer
add import
generalise summary function
move summary function determination out of loop
start to use iterators