Permutation works, but many bugs