Added permutation function and improved the tests