diff --git a/quad/src/computation_graph/computation_graph.c b/quad/src/computation_graph/computation_graph.c
index 8720fcb9851edaea00864533d852dfb7653eee95..6422d5b11aa95df89dcdb57a3c82b9d1f4ce047a 100644
--- a/quad/src/computation_graph/computation_graph.c
+++ b/quad/src/computation_graph/computation_graph.c
@@ -11,9 +11,9 @@ static double exec_input_vals[GRAPH_MAX_INPUTS];
 
 // Macro functions for setting and clearing single bits in int array
 // From http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html
-#define setBit(A,k)     ( A[(k / sizeof(int))] |=  (1 << (k % sizeof(int))) )
-#define clearBit(A,k)   ( A[(k / sizeof(int))] &= ~(1 << (k % sizeof(int))) )
-#define testBit(A,k)    ( A[(k / sizeof(int))] &   (1 << (k % sizeof(int))) )
+#define setBit(A,k)     ( A[(k / (8*sizeof(int)))] |=  (1 << (k % (8*sizeof(int)))) )
+#define clearBit(A,k)   ( A[(k / (8*sizeof(int)))] &= ~(1 << (k % (8*sizeof(int)))) )
+#define testBit(A,k)    ( A[(k / (8*sizeof(int)))] &   (1 << (k % (8*sizeof(int)))) )
 
 struct computation_graph *create_graph() {
     struct computation_graph *the_graph = malloc(sizeof(struct computation_graph));
@@ -120,16 +120,16 @@ int graph_add_node_id(struct computation_graph *graph,
         if (!node_arr) { return -1; }
         // Number of integers needed to hold new_size bits
         size_t new_exist_size = ceil((float)new_size / (8 * sizeof(int))); // ceil(new_size / (bits per int))
-        int* exist_arr = realloc(graph->node_existence, sizeof(int) * new_exist_size);
-        if (!exist_arr) {return -1;}
         // Set the newly allocated memory to 0
         size_t old_exist_size = ceil((float)old_size / (8 * sizeof(int)));
         if (old_exist_size != new_exist_size) {
+            int* exist_arr = realloc(graph->node_existence, sizeof(int) * new_exist_size);
+            if (!exist_arr) {return -1;}
             memset(exist_arr + old_exist_size, 0, (new_exist_size - old_exist_size) * sizeof(int));
+            graph->node_existence = exist_arr;
         }
         graph->size = new_size;
         graph->nodes = node_arr;
-        graph->node_existence = exist_arr;
     }
     struct graph_node *new_node = &graph->nodes[id];
     new_node->name = strdup(name);