diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java
index 59c63a0bd..c738b5166 100644
--- a/app/src/processing/app/Sketch.java
+++ b/app/src/processing/app/Sketch.java
@@ -131,13 +131,12 @@ public class Sketch {
    */
   public void handleRenameCode() {
     SketchCode current = editor.getCurrentTab().getSketchCode();
-    int currentIndex = editor.getCurrentTabIndex();
 
     editor.status.clearState();
     // make sure the user didn't hide the sketch folder
     ensureExistence();
 
-    if (currentIndex == 0 && editor.untitled) {
+    if (current.isPrimary() && editor.untitled) {
       Base.showMessage(tr("Sketch is Untitled"),
                        tr("How about saving the sketch first \n" +
                          "before trying to rename it?"));
@@ -157,7 +156,7 @@ public class Sketch {
     // ask for new name of file (internal to window)
     // TODO maybe just popup a text area?
     renamingCode = true;
-    String prompt = (currentIndex == 0) ?
+    String prompt = current.isPrimary() ?
       "New name for sketch:" : "New name for file:";
     String oldName = (current.isExtension("ino")) ? current.getPrettyName()
                                                   : current.getFileName();
@@ -263,7 +262,7 @@ public class Sketch {
       return;
     }
 
-    if (renamingCode && currentIndex == 0) {
+    if (renamingCode && current.isPrimary()) {
       for (SketchCode code : data.getCodes()) {
         if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) &&
           code.isExtension("cpp")) {
@@ -296,7 +295,7 @@ public class Sketch {
 //    }
 
     if (renamingCode) {
-      if (currentIndex == 0) {
+      if (current.isPrimary()) {
         // get the new folder name/location
         String folderName = newName.substring(0, newName.indexOf('.'));
         File newFolder = new File(data.getFolder().getParentFile(), folderName);
@@ -396,7 +395,7 @@ public class Sketch {
         return;
       }
       ensureExistence();
-      SketchCode code = new SketchCode(newFile);
+      SketchCode code = new SketchCode(newFile, false);
       try {
         editor.addTab(code, "");
       } catch (IOException e) {
@@ -425,7 +424,6 @@ public class Sketch {
    */
   public void handleDeleteCode() throws IOException {
     SketchCode current = editor.getCurrentTab().getSketchCode();
-    int currentIndex = editor.getCurrentTabIndex();
     editor.status.clearState();
     // make sure the user didn't hide the sketch folder
     ensureExistence();
@@ -442,7 +440,7 @@ public class Sketch {
 
     // confirm deletion with user, yes/no
     Object[] options = { tr("OK"), tr("Cancel") };
-    String prompt = (currentIndex == 0) ?
+    String prompt = current.isPrimary() ?
       tr("Are you sure you want to delete this sketch?") :
       I18n.format(tr("Are you sure you want to delete \"{0}\"?"),
                   current.getFileNameWithExtensionIfNotIno());
@@ -455,7 +453,7 @@ public class Sketch {
                                               options,
                                               options[0]);
     if (result == JOptionPane.YES_OPTION) {
-      if (currentIndex == 0) {
+      if (current.isPrimary()) {
         // need to unset all the modified flags, otherwise tries
         // to do a save on the handleNew()
 
@@ -856,7 +854,7 @@ public class Sketch {
     }
 
     if (codeExtension != null) {
-      SketchCode newCode = new SketchCode(destFile);
+      SketchCode newCode = new SketchCode(destFile, false);
 
       if (replacement) {
         data.replaceCode(newCode);
diff --git a/arduino-core/src/processing/app/SketchCode.java b/arduino-core/src/processing/app/SketchCode.java
index 12f9fd680..06d2d3267 100644
--- a/arduino-core/src/processing/app/SketchCode.java
+++ b/arduino-core/src/processing/app/SketchCode.java
@@ -45,6 +45,11 @@ public class SketchCode {
    */
   private File file;
 
+  /**
+   * Is this the primary file in the sketch?
+   */
+  private boolean primary;
+
   /**
    * Interface for an in-memory storage of text file contents. This is
    * intended to allow a GUI to keep modified text in memory, and allow
@@ -71,8 +76,17 @@ public class SketchCode {
    */
   private TextStorage storage;
 
-  public SketchCode(File file) {
+  /**
+   * Create a new SketchCode
+   *
+   * @param file
+   *          The file this SketchCode represents
+   * @param primary
+   *          Whether this file is the primary file of the sketch
+   */
+  public SketchCode(File file, boolean primary) {
     this.file = file;
+    this.primary = primary;
   }
 
   /**
@@ -88,6 +102,12 @@ public class SketchCode {
     return file;
   }
 
+  /**
+   * Is this the primary file in the sketch?
+   */
+  public boolean isPrimary() {
+    return primary;
+  }
 
   protected boolean fileExists() {
     return file.exists();
diff --git a/arduino-core/src/processing/app/SketchData.java b/arduino-core/src/processing/app/SketchData.java
index b56685815..85f70bd77 100644
--- a/arduino-core/src/processing/app/SketchData.java
+++ b/arduino-core/src/processing/app/SketchData.java
@@ -134,7 +134,8 @@ public class SketchData {
           // Don't allow people to use files with invalid names, since on load,
           // it would be otherwise possible to sneak in nasty filenames. [0116]
           if (BaseNoGui.isSanitaryName(base)) {
-            addCode(new SketchCode(new File(folder, filename)));
+            File file = new File(folder, filename);
+            addCode(new SketchCode(file, file.equals(primaryFile)));
           } else {
             System.err.println(I18n.format(tr("File name {0} is invalid: ignored"), filename));
           }