Turned nested loops inside out.