Stop using itertools by implementing permutations & powerset functions