changing all_perms to all_perms_gen (uses a generator to save memory)