remove code duplication
add a couple of consts to range based for loops