Fixed permutations code (and tested it)