Changed ugly for loop for itertools.permutations