nested loops -> factor into separate function.