--- a/nouveau-pm/tools/extract.c
+++ b/nouveau-pm/tools/extract.c
@@ -1,83 +1,99 @@
-#
-#  This is a small program that splits the dumps into sections
-# correcponding to the different periods in the downclocking
-# The file info.txt shows the frequencies, and the dumps that
-# contain information about it
-#
-# XXX: Take the input name from the command line
-# XXX: Simplify this using a script?
+/*
+  This is a small program that splits the dumps into sections
+ correcponding to the different periods in the downclocking
+ The file info.txt shows the frequencies, and the dumps that
+ contain information about it
+
+ XXX: Simplify this using a script?
+*/
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
-int main(void)
+FILE *srcFile, *outputFile, * infoFile;
+
+void fileio_error(char *filename, char mode)
 {
-	FILE *srcFile, *outputFile, *infoFile;
-	char mark_str[100], *nv_str, outputfile_name[255];
-	char *data, buffer[100], *ret, *nv_available, *perf_list_available;
-	int counter, start, is_mark;
+	printf("Error while trying to %s %s\n", ((mode = 'w') ? "write to" : "read from"), filename);
+	if (outputFile)
+		fclose(outputFile);
+	if (infoFile)
+		fclose(infoFile);
+	if (srcFile)
+		fclose(srcFile);
+}
 
-	counter = start = is_mark = 0;
+int main(int argc, char **argv)
+{
+	char mark_str[100], *nv_str, input_filename[255], output_filename[255];
+	char *data, buffer[200], *ret, *clock_freq_available, *perf_list_available;
+	int i, start_to_dump, not_mark, not_unknown;
 
-	srcFile = fopen("mydump.txt","r");
-	if ( srcFile == NULL)
-	{
-		printf("Cannot open file\n");
+	i = start_to_dump = 0;
+	not_mark = not_unknown = 0;
+
+	if (argc != 2) {
+		printf("Argument count mismatch\nUsage \"%s mmio-dump\"", argv[0]);
+		return(1);
+	}
+
+	sscanf (argv[1], "%s", input_filename);
+	srcFile = fopen(input_filename,"r");
+	if ( !srcFile ) {
+		fileio_error(input_filename, 'r');
 		return(1);
 	}
 
 	infoFile = fopen("info.txt","w");
-	if ( infoFile == NULL)
-	{
-		printf("Cannot info create file\n");
-		fclose(srcFile);
+	if ( !infoFile ) {
+		fileio_error("info.txt", 'r');
 		return(1);
 	}
 
-	ret = fgets(buffer, 100, srcFile);
 	outputFile = NULL;
-
-	while (( ret != NULL ) && ( !feof(srcFile) ))
-	{
+	while ((!feof(srcFile)) & (fgets(buffer, 200, srcFile)!=0)) {
 		sscanf( buffer , "%s", mark_str);
-		is_mark = strncmp(mark_str,"MARK",4);
+		not_mark = strncmp(mark_str,"MARK",4);
+		not_unknown = strncmp(mark_str,"UNKNOWN",7);
 		nv_str = strstr( buffer, "NV_" );
 
-		if (( is_mark != 0 ) && ( start == 1 ))
-		{
-			fputs( buffer, outputFile);
-		}
-		if ((is_mark == 0) && ( nv_str != NULL ))
-		{
+		if (not_mark) {
+			if (start_to_dump)
+			// Current line is not a MARK and the output file has been open
+				fputs( buffer, outputFile);
+		} else if (nv_str) {
+			// Current line contains "NV_"
 			perf_list_available = strstr( buffer, "NV_CTRL_STRING_PERFORMANCE_MODES");
-			if ( perf_list_available != NULL) 
+			if (perf_list_available)
+				// Dump the list of perf modes to the infofile
 				fprintf( infoFile,"%s", perf_list_available );
-			nv_available = strstr( buffer, "NV_CTRL_GPU_CURRENT_CLOCK_FREQS");
-			if ( nv_available != NULL)
-			{
-				fprintf( infoFile,"%s >>>>>> %i.txt <<<<<<\n", nv_available, counter+1 );
-				if (outputFile != NULL )
-				{
+
+			clock_freq_available = strstr( buffer, "NV_CTRL_GPU_CURRENT_CLOCK_FREQS");
+			if (clock_freq_available) {
+				// Close any previous outputfile
+				if (outputFile)
 					fclose(outputFile);
-				}
-				counter++;
-				sprintf( outputfile_name, "%d.txt", counter );
-				outputFile = fopen(outputfile_name,"w");
-				if ( outputFile == NULL)
-				{
-					printf("Cannot create file\n");
-					fclose(srcFile);
-					fclose(infoFile);
+				i++;
+				// And open a new one
+				sprintf( output_filename, "transition-%d", i );
+				outputFile = fopen(output_filename,"w");
+				fprintf( infoFile,"%s >>>>>> %s <<<<<<\n", clock_freq_available, output_filename );
+				if ( !outputFile ) {
+					fileio_error(output_filename, 'w');
 					return(1);
 				}
-				start = 1;
+				printf("File %s opened\n", output_filename);
+				start_to_dump = 1;
 			}
 		}
-		ret = fgets(buffer, 200, srcFile);
 	}
 
-    if (outputFile != NULL) fclose(outputFile);
-    if (infoFile != NULL) fclose(infoFile);
-    if (srcFile != NULL) fclose(srcFile);
+    if (outputFile)
+		fclose(outputFile);
+    if (infoFile)
+		fclose(infoFile);
+    if (srcFile)
+		fclose(srcFile);
     return 0;
 }