Bugfix for renaming of faces
diff --git a/tools/alignments.py b/tools/alignments.py
index 6a1cbe498b..4ae27ab8be 100644
--- a/tools/alignments.py
+++ b/tools/alignments.py
@@ -38,8 +38,9 @@ def process(self):
             job = Draw(self.alignments, self.args)
         elif self.args.job == "extract":
             job = Extract(self.alignments, self.args)
-        elif self.args.job in("missing-alignments", "missing-frames", "leftover-faces",
-                              "multi-faces", "no-faces"):
+        elif self.args.job in("missing-alignments", "missing-frames",
+                              "multi-faces", "leftover-faces",
+                              "no-faces"):
             job = Check(self.alignments, self.args)
         elif self.args.job == "remove":
             job = RemoveAlignments(self.alignments, self.args)
@@ -172,8 +173,7 @@ def __init__(self, folder, verbose):
         self.verbose = verbose
         self.folder = folder
         self.check_folder_exists()
-        self.file_list_sorted = sorted([item
-                                        for item in self.process_folder()])
+        self.file_list_sorted = self.sorted_items()
         self.items = self.load_items()
         self.count = len(self.file_list_sorted)
         if self.verbose:
@@ -194,6 +194,11 @@ def valid_extension(filename):
         extension = os.path.splitext(filename)[1]
         return bool(extension in _image_extensions)
 
+    @staticmethod
+    def sorted_items():
+        """ Override for specific folder processing """
+        return list()
+
     @staticmethod
     def process_folder():
         """ Override for specific folder processing """
@@ -243,6 +248,11 @@ def load_items(self):
                 faces[original_file].append(index)
         return faces
 
+    def sorted_items(self):
+        """ Return the items sorted by filename then index """
+        return sorted([item for item in self.process_folder()],
+                      key=lambda x: (x[2], x[3]))
+
 
 class Frames(MediaLoader):
     """ Object to hold the frames that are to be checked against """
@@ -264,6 +274,10 @@ def load_items(self):
                              frame[frame.rfind("."):])
         return frames
 
+    def sorted_items(self):
+        """ Return the items sorted by filename """
+        return sorted([item for item in self.process_folder()])
+
 
 class Draw(object):
     """ Draw Alignments on passed in images """
@@ -444,7 +458,7 @@ def process(self):
     def faces_count_matches(self, item):
         """ Check the selected face exits """
         image_name, number_alignments = item[0], item[2]
-        number_faces = len(self.faces.items.get(image_name, []))
+        number_faces = len(self.faces.items.get(image_name, None))
         return bool(number_alignments == 0
                     or number_alignments == number_faces)
 
@@ -526,10 +540,10 @@ def __init__(self, alignments, arguments):
 
     def get_source_dir(self, arguments):
         """ Set the correct source dir """
-        if hasattr(arguments, "faces_dir"):
+        if hasattr(arguments, "faces_dir") and arguments.faces_dir:
             self.type = "faces"
             source_dir = arguments.faces_dir
-        elif hasattr(arguments, "frames_dir"):
+        elif hasattr(arguments, "frames_dir") and arguments.frames_dir:
             self.type = "frames"
             source_dir = arguments.frames_dir
         else:
@@ -559,9 +573,11 @@ def validate(self):
                   "there will be nothing to move. "
                   "Defaulting to output: console")
             self.output = "console"
-        elif self.type == "faces" and self.job != "multi-faces" and self.job != "leftover-faces":
-            print("WARNING: The selected folder is not valid. Only folder set "
-                  "with '-fc' is supported for 'multi-faces' and 'leftover-faces'")
+        elif self.type == "faces" and self.job not in ("multi-faces",
+                                                       "leftover-faces"):
+            print("WARNING: The selected folder is not valid. "
+                  "Only folder set with '-fc' is supported for "
+                  "'multi-faces' and 'leftover-faces'")
             exit(0)
 
     def compile_output(self):
@@ -617,10 +633,9 @@ def get_leftover_faces(self):
         for item in self.items:
             frame_id = item[2] + item[1]
 
-            if frame_id not in self.alignments_data or self.alignments_data[frame_id] <= item[3]:
+            if (frame_id not in self.alignments_data
+                    or self.alignments_data[frame_id] <= item[3]):
                 yield item[0] + item[1]
-            
-        print("Done")
 
     def output_results(self):
         """ Output the results in the requested format """
diff --git a/tools/cli.py b/tools/cli.py
index 9c4de18451..478521bcd4 100644
--- a/tools/cli.py
+++ b/tools/cli.py
@@ -20,8 +20,9 @@ def get_argument_list(self):
                               "type": str,
                               "choices": ("draw", "extract",
                                           "missing-alignments",
-                                          "missing-frames", "leftover-faces", "multi-faces",
-                                          "no-faces", "reformat", "remove"),
+                                          "missing-frames", "leftover-faces",
+                                          "multi-faces", "no-faces",
+                                          "reformat", "remove"),
                               "required": True,
                               "help": "R|Choose which action you want to "
                                       "perform.\nNB: All actions require an "

An issue occurred when more than 10 faces were found within a frame. On removing frames and renaming, indexes were not processed in the correct order, meaning that files could potentially be renamed to existing file names. This fixes that bug