public static void main(String[] args) {

    // Default values
    int numInputFiles = 2;
    String tempDirectory = System.getProperty("user.dir");
    String outputDirectory = "";
    String inputDirectory = "";

    // Readers
    BufferedReader inputReader = null;
    BufferedReader[] bufferedReaders = null;
    FileReader fr = null;

    // Writers
    BufferedWriter[] bufferedWriters = null;
    int currentFile = 0;

    // Files for input/output of runs
    File[] inputs = null;
    File[] outputs = null;

    int comparisonsCount = 0;
    int runsCount = 0;
    int passesCount = 0;

    try {

      System.out.println();

      // Get the arguments if they exist
      if (args.length == 1) {
        inputDirectory = args[args.length - 1];
      } else if (args.length > 1) {

        for (int i = 0; i < args.length; i++) {
          if (args[i].equals("-k")) {
            if (isNumeric(args[i + 1])) {
              // Check for any value above 2 (the default)
              if (Integer.parseInt(args[i + 1]) > 2) {
                // Get the number of input files
                numInputFiles = Integer.parseInt(args[i + 1]);
              }
            }
          } else if (args[i].equals("-t")) {
            tempDirectory = args[i + 1];
          } else if (args[i].equals("-o")) {
            outputDirectory = args[i + 1];
          }
        }

        System.out.println(args[args.length - 2]);
        if (!args[args.length - 2].contains("-")) {
          inputDirectory = args[args.length - 1];
        }
      }

      System.out.println("Number of input files:" + numInputFiles);
      System.out.println("Temporary directory:" + tempDirectory);
      System.out.println("Output directory:" + outputDirectory);
      System.out.println("Input directory:" + inputDirectory);
      System.out.println();

      // Create array of temp Files
      inputs = new File[numInputFiles];
      // Need the same number of writers as input files
      bufferedWriters = new BufferedWriter[numInputFiles];
      bufferedReaders = new BufferedReader[numInputFiles];

      String filePrefix = "tmp_";
      // Create the array of input files
      for (int i = 0; i < numInputFiles; i++) {
        inputs[i] = new File(tempDirectory + "/" + filePrefix + i + ".txt");
        inputs[i].createNewFile();
        bufferedWriters[i] = new BufferedWriter(new FileWriter(inputs[i].getAbsoluteFile()));
      }

      inputReader = new BufferedReader(new FileReader(inputDirectory));

      // Start creating the runs
      String[] run = new String[15];
      for (int i = 0; i < 15; i++) {
        // Put the first 15 elements from raw data into the array
        if (inputReader.ready()) run[i] = inputReader.readLine();
      }

      // Build the priority queue by passing it the unsorted array
      MinHeap heap = new MinHeap(run);
      System.out.print("initial unsorted heap:");
      heap.traverse();
      heap.heapify();
      System.out.print("sorted heap:");
      heap.traverse();

      // Count on the temp file which we are writing to
      int tempWriteCounter = 0;
      String line = "";
      boolean endOfStream = false;

      // While the heap is not empty
      while (!heap.isEmpty() && !endOfStream) {
        // Write minimum to output
        String min = heap.remove();
        System.out.println("wrote: " + min + " to output");
        bufferedWriters[tempWriteCounter].write(min);
        bufferedWriters[tempWriteCounter].newLine();

        // Make sure reader is ready
        if (inputReader.ready() && !endOfStream) {
          // If there are more elements to read in
          // if((line = inputReader.readLine()) != null){
          line = inputReader.readLine();
          System.out.println("read in: " + line);
          // If the new input is smaller than the last element output to file,
          // add it to the dead space of the heap using .append(line);
          if (MinHeap.precedesValue(line, min)) {
            System.out.println(line + " is less than " + min + ", so append " + line);
            heap.append(line);
          } else {
            System.out.println(line + " is greater than " + min + ", so insert " + line);
            heap.insert(line);
          }
        } else {
          endOfStream = true;
          System.out.println("Reached end of stream-1");
        }

        heap.traverse();

        // If the heap is empty AND there are more elements to read in,
        // fill the heap up, heapify()and write to next temp file
        if (heap.isEmpty() && !endOfStream) {
          System.out.println("HEAP IS NOW EMPTY... begin next run");

          // Find out how many new elements need to be added to fill the heap
          int numElements = heap.getEmptyCells();

          // Create the array of appropriate size
          String[] remaining = new String[numElements];

          System.out.println("number of new elements needed: " + numElements);

          // Index for the new array
          int i = 0;
          // While there are more elements to read in AND there are still empty spaces in the heap
          while (!endOfStream && i < numElements) {
            if (inputReader.ready()) {
              line = inputReader.readLine();
              System.out.println("read in: " + line);
              remaining[i] = line;
            } else {
              endOfStream = true;
              System.out.println("Reached end of stream-2");
            }
          }

          // Fill the empty spaces in the heap
          heap.fill(remaining);
          heap.heapify();
          heap.traverse();

          // Proceed to the next temp file
          tempWriteCounter++;
        }
      }

      inputReader.close();

      // Close the writers
      for (int i = 0; i < bufferedWriters.length; i++) {
        bufferedWriters[i].close();
        // bufferedReaders[i].close();
      }

      System.out.println();

    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
    }

    // System.out.println();

    // Priority Queue
    MinHeap minHeap = new MinHeap();

    // Stuff to show that insert() works
    /*
    minHeap.insert("d");

    System.out.println();
    minHeap.traverse();
    System.out.println();

    minHeap.insert("b");

    System.out.println();
    minHeap.traverse();
    System.out.println();

    minHeap.insert("e");

    System.out.println();
    minHeap.traverse();
    System.out.println();

    minHeap.insert("c");

    System.out.println();
    minHeap.traverse();
    System.out.println();

    minHeap.insert("a");

    System.out.println();
    minHeap.traverse();
    System.out.println();

    minHeap.remove();

    System.out.println();
    minHeap.traverse();
    System.out.println();

    minHeap.remove();

    System.out.println();
    minHeap.traverse();
    System.out.println();

    minHeap.remove();

    System.out.println();
    minHeap.traverse();
    System.out.println();

    */

    // Stuff to show that heapify() works
    /*
    String[] array = new String[14];
    array[0] = "n";
    array[1] = "i";
    array[2] = "e";
    array[3] = "l";
    array[4] = "b";
    array[5] = "h";
    array[6] = "d";
    array[7] = "a";
    array[8] = "k";
    array[9] = "o";
    array[10] = "j";
    array[11] = "c";
    array[12] = "f";
    array[13] = "g";
    minHeap = new MinHeap(array);
    minHeap.traverse();
    System.out.println();
    minHeap.heapify();
    minHeap.traverse();
    */

    System.out.println();
  }