permutations algorithm worked fine