First version of permute function made by myself