Index: /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp
===================================================================
--- /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 444)
+++ /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 445)
@@ -88,9 +88,30 @@
 {
     for(size_t i = 0; i < argc; i++) {
-        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
-    }
-
-    printf("\n");
+        LOG("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+
+    LOG("\n");
     return 0;
+}
+
+/* Useful for spotchecking what's in the database */
+void
+printDatabase() 
+{
+    /*
+    LOG("\n\n\n");
+    strcpy(_data_DB->command, "select ");
+    strcat(_data_DB->command, _data_DB->tablename);
+    strcat(_data_DB->command, ".* from ");
+    strcat(_data_DB->command, _data_DB->tablename);
+    strcat(_data_DB->command, ";");
+
+    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
+    if((rc != SQLITE_OK) && (rc != 101))
+        WARNING("SQL error: %s\n", errorMsg);
+
+    LOG("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename);
+    LOG("\n\n\n");
+    */
 }
 
@@ -628,5 +649,5 @@
                         WARNING("services_DB:: Mission requires service %s ", \
                                 miss[activeMission].services[i].name.c_str());
-                        WARNING("not provided by any connected component.\n")
+                        WARNING("not provided by any connected component.\n");
                         rc = 31337;
                     }
@@ -720,112 +741,5 @@
     SendMessage(miss[activeMission].services[sourceID].socketFD, \
             miss[activeMission].services[sourceID].name.c_str());
-
-   /* If the service takes a parameter, feed that parameter in
-   if(!miss[activeMission].services[sourceID].parameter.empty()){
-    //printf("sending parameter!\n");
-    SendMessage(miss[activeMission].services[sourceID].socketFD, "1");
-    SendMessage(miss[activeMission].services[sourceID].socketFD, "parameter");
-    SendMessage(miss[activeMission].services[sourceID].socketFD, miss[activeMission].services[sourceID].parameter.c_str());
-    }
-
-
-    //Load and transmit the input data
-    int i = 0;
-    while(i < 10 && !miss[activeMission].services[sourceID].input[i].empty()){
-        _data_DB->command="select ";
-       _data_DB->command.append(_data_DB->tablename);
-        _data_DB->command.append(".* from ");
-        _data_DB->command.append(_data_DB->tablename);
-        _data_DB->command.append(" where Tag=='");
-    _data_DB->command.append(miss[activeMission].services[sourceID].input[i]);
-        _data_DB->command.append("';");
-        sqlite3_stmt * pStatement;
-        int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-        if (rc == SQLITE_OK){ 
-            if (sqlite3_step(pStatement) == SQLITE_ROW)
-             data.append((const char*) sqlite3_column_text(pStatement, 1)); 
-            else {
-                printf("2data_DB:: Data not yet in DB.\n");
-            rc=31337;
-        }
-        } 
-    else {
-        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-        }
-        sqlite3_finalize(pStatement);
-
-        token = strtok((char *)data.c_str(), "@");
-    while(token){
-        SendMessage(miss[activeMission].services[sourceID].socketFD, token);
-        token = strtok(NULL, "@");
-    }
-    i++;
-    data.clear();
-    }
-    int32_t j = 0;
-    
-    //Receive and store the output data
-    while(j < 10 && !miss[activeMission].services[sourceID].output[j].empty()){
-    int rc;
-    memset(buffer, 0, 256);
-    //Read the number of datapairs for this output
-    ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer);
-    data.append(buffer);
-    data.append("@");
-    int t = atoi(buffer);
-    for(int k = 0; k < t; k++){
-        //Read the datapairs incrementally and deliminate it with the "@" symbol
-        memset(buffer, 0, 256);
-        ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer);
-        data.append(buffer);
-        data.append("@");
-        memset(buffer, 0, 256);
-        ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer);
-        data.append(buffer);
-        data.append("@");
-    }
-
-    _data_DB->command="insert or replace into ";
-        _data_DB->command.append(_data_DB->tablename); 
-          _data_DB->command.append(" (");
-        _data_DB->command.append(cols[0]);
-        _data_DB->command.append(", ");
-    _data_DB->command.append(cols[1]);
-    _data_DB->command.append(") ");
-    _data_DB->command.append(" values('");    
-    _data_DB->command.append(miss[activeMission].services[sourceID].output[j]);
-    _data_DB->command.append("', '");
-    _data_DB->command.append(data);
-    _data_DB->command.append("');");
-        char *errorMsg;
-       rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
-       if( rc!=SQLITE_OK && rc!=101 )
-           fprintf(stderr, "SQL error: %s\n", errorMsg);
-        //printf("S: done putting ouptut data into DB for ID#='%d', data=%s\n", sourceID, data.c_str());
-    j++;
-        data.clear();
-    }
-    //printf("done transact data!\n");
-   // LOG("ServiceManagementLayer:: Finished with data transaction.\n");
-
-
-    /*printf("\n\n\n");
-    // generate commandi
-    strcpy(_data_DB->command, "select ");
-    strcat(_data_DB->command, _data_DB->tablename);
-    strcat(_data_DB->command, ".* from ");
-    strcat(_data_DB->command, _data_DB->tablename);
-    strcat(_data_DB->command, ";");
-
-    // execute print (select all)  command    
-    char *errorMsg;
-    int rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
-    if( rc!=SQLITE_OK && rc!=101 )
-        fprintf(stderr, "SQL error: %s\n", errorMsg);
-    printf("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename);
-    printf("\n\n\n");*/
-}
-
-
+}
 
 /* CALLED BY: MessageHandler
@@ -833,283 +747,203 @@
  * OUTPUTS: <none>
  *
- * DESCRIPTION: This function works by first sending the inputs from the shell to the appropriate components
- * The first service should begin immeadiately, as should any others who have all of their input parameters
- * When they complete, the output path is found and the data is transfered as it becomes available
- * Presumably at this point the second function has all of it's parameters, so it begins to compute, and the cycle repeats
- *  
+ * DESCRIPTION: This function works by first sending the inputs from the shell to 
+ * the appropriate components. The first service should begin immeadiately, as 
+ * should any others who have all of their input parameters. When they complete, 
+ * the output path is found and the data is transfered as it becomes available
+ * Presumably at this point the second function has all of it's parameters, so it 
+ * begins to compute, and the cycle repeats.
  * 
  * Rules for active missions (currently)
  * -Five inputs/outputs per service and per mission
- * -All ordering constraints have been relaxed in this version; all data is stored locally and only sent when requested
+ * -All ordering constraints have been relaxed in this version; all data is stored 
+ *  locally and only sent when requested
  * -If and while support fully implemented - up to three levels (if's can be nested, but dowhiles cannot)
  * -For dowhiles, assumes loop condition determined on last line
- * -IMPORTANT: DB uses '@' to seperate individual statements; using '@' in the data stream will result in incorrect behavior
- */
-
-//WHILE
+ *
+ * -IMPORTANT: DB uses '@' to seperate individual statements; using '@' in the data 
+ *  stream will result in incorrect behavior
+ */
 void
 ServiceManagementLayer::PerformActiveMission()
 {
-    //printf("start PAM\n");
-    uint16_t i = 0, t;
     shellFound = false;
-    std::string data_param, data_obsv, data;
+    std::string data_param;
+    std::string data_obsv;
+    std::string data;
     std::string input;
     std::string check;
+
     char buffer[256];
     char buffer1[256];
     std::string token, token2;
     std::string data2;
-    int rc;
+
+    int32_t rc;
     char *errorMsg;
-    char* cols[] = {(char *)"Tag", (char *)"Data"};
+    char *cols[] = {(char *) "Tag", (char *) "Data"};
 
     LOG("ServiceManagementLayer:: Received PerformActiveMission command.\n");
 
-    //Get the inputs
+    /* Get the inputs */
     memset(buffer, 0, 256);
-    //Needed to read the zero passed in by GOP as the number of observables
     ReadMessage(shellSocketFD, buffer);
-
 
     /* Receive Set of Parameters */
     memset(buffer, 0, 256);
     ReadMessage(shellSocketFD, buffer);
-    t=atoi(buffer);
-    for(int m = 0; m < t; m++) {
+    int32_t t = atoi(buffer);
+    for(size_t m = 0; m < t; m++) {
         memset(buffer1, 0, 256);
         ReadMessage(shellSocketFD, buffer1);
-    _data_DB->command="insert into ";
+        _data_DB->command="insert into ";
         _data_DB->command.append(_data_DB->tablename); 
-          _data_DB->command.append(" (");
+        _data_DB->command.append(" (");
         _data_DB->command.append(cols[0]);
         _data_DB->command.append(", ");
-    _data_DB->command.append(cols[1]);
-    _data_DB->command.append(") ");
+        _data_DB->command.append(cols[1]);
+        _data_DB->command.append(") ");
+
         memset(buffer, 0, 256);
         ReadMessage(shellSocketFD, buffer);
-    _data_DB->command.append(" values('");
-    _data_DB->command.append(buffer1);
-    _data_DB->command.append("', '1@");
-    _data_DB->command.append(buffer1);
-    _data_DB->command.append("@");
-    _data_DB->command.append(buffer);
-    _data_DB->command.append("');");
-       rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
-       if( rc!=SQLITE_OK && rc!=101 )
-           fprintf(stderr, "SQL error: %s\n", errorMsg);
-    }
-
-    //Useful for spotchecking what's in the database
-    /*printf("\n\n\n");
-    // generate commandi
-    strcpy(_data_DB->command, "select ");
-    strcat(_data_DB->command, _data_DB->tablename);
-    strcat(_data_DB->command, ".* from ");
-    strcat(_data_DB->command, _data_DB->tablename);
-    strcat(_data_DB->command, ";");
-
-    // execute print (select all)  command  
-    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
-    if( rc!=SQLITE_OK && rc!=101 )
-        fprintf(stderr, "SQL error: %s\n", errorMsg);
-    printf("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename);
-    printf("\n\n\n");*/
-
-
-    i=0; 
-    int32_t numstatements[3] = {0,0,0};
-    while(i < miss[activeMission].numServices)
-    {
-    if(miss[activeMission].services[i].name.compare("if")==0)
-    {
-       //printf("L0:if detected\n");
-        input.clear();
-        check.clear();
-        int t;
-        for(t = 0; t < 10; t++){
-        if(!miss[activeMission].services[i].output[t].empty()){
-            input=miss[activeMission].services[i-numstatements[0]-1].output[t];
-            _data_DB->command="SELECT ";
-            _data_DB->command.append(_data_DB->tablename);
-            _data_DB->command.append(".* from ");
-            _data_DB->command.append(_data_DB->tablename);
-            _data_DB->command.append(" where Tag=='");
-            _data_DB->command.append(input);
-            _data_DB->command.append("';");
-            sqlite3_stmt * pStatement;
-            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-            if (rc == SQLITE_OK){ 
-            if (sqlite3_step(pStatement) == SQLITE_ROW)
-                 data = (const char *) sqlite3_column_text(pStatement, 1); 
-            else {
-                printf("1 data_DB:: Data not yet in DB.\n");
-                rc=31337;
-            }
-            } else {
-            printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-            }
-                sqlite3_finalize(pStatement);
-            //printf("data=%s\n", data.c_str());
-            int pos = data.find_last_of("@", data.length()-2);
-            token = data.substr(pos+1);
-            token.erase(token.length()-1);
-            //printf("token=%s, %d\n", token.c_str(), pos);
-        data.clear();
-            break;
-        }
-        }
-        //printf("Level 0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
-        bool doit = false;
-        if(miss[activeMission].services[i].output[t].find(">") != string::npos){
-            std::string data2;
-            _data_DB->command="SELECT ";
-            _data_DB->command.append(_data_DB->tablename);
-            _data_DB->command.append(".* from ");
-            _data_DB->command.append(_data_DB->tablename);
-            _data_DB->command.append(" where Tag=='");
-            _data_DB->command.append(miss[activeMission].services[i].output[t].erase(0, 1));
-            _data_DB->command.append("';");
-            sqlite3_stmt * pStatement;
-            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-            if (rc == SQLITE_OK){ 
-            if (sqlite3_step(pStatement) == SQLITE_ROW)
-                 data2 = (const char *) sqlite3_column_text(pStatement, 1); 
-            else {
-                printf("2 data_DB:: Data not yet in DB.\n");
-                rc=31337;
-            }
-            } else {
-            printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-            }
-            sqlite3_finalize(pStatement);
-
-            int pos = data2.find_last_of("@", data2.length()-2);
-            token2 = data2.substr(pos+1);
-            token2.erase(token2.length()-1);
-            //printf("token2=%s, %d\n", token2.c_str(), pos);
-            if(atof(token.c_str()) > atof(token2.c_str()))
-            doit=true;
-            //printf("%s %s\n", buffer, token);
-        }
-        else if (miss[activeMission].services[i].output[t].find(token) != string::npos)
-                doit=true;
-        if(doit){
-        //printf("Level 0:if taken\n");
-        for(uint16_t k = i+1; k <= i+miss[activeMission].services[i].num_conds; k++){ 
-            if(miss[activeMission].services[k].name.compare("if")==0){
-            //printf("Level 1:if detected\n");
-                input.clear();
-                check.clear();
-                for(t = 0; t < 10; t++){
-                if(!miss[activeMission].services[k].output[t].empty()){
-                    input=miss[activeMission].services[k-numstatements[1]-1].output[t];
-                        _data_DB->command="SELECT ";
-                        _data_DB->command.append(_data_DB->tablename);
-                       _data_DB->command.append(".* from ");
-                       _data_DB->command.append(_data_DB->tablename);
-                      _data_DB->command.append(" where Tag=='");
-                     _data_DB->command.append(input);
-                    _data_DB->command.append("';");
-                    sqlite3_stmt * pStatement;
-                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-                    if (rc == SQLITE_OK){ 
-                    if (sqlite3_step(pStatement) == SQLITE_ROW)
-                         data = (const char *) sqlite3_column_text(pStatement, 1); 
-                    else {
-                        printf("3 data_DB:: Data not yet in DB.\n");
-                        rc=31337;
-                    }
-                    } else {
-                    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-                    }
-                        sqlite3_finalize(pStatement);
-                    //printf("Level 1:data=%s\n", data.c_str());
-                    int pos = data.find_last_of("@", data.length()-2);
-                    token = data.substr(pos+1);
-                    token.erase(token.length()-1);
-                    //printf("Level 1:token=%s\n", token);
-                    break;
-                }
-                }
-                bool doit = false;
-                if(miss[activeMission].services[k].output[t].find(">") != string::npos){
-                    std::string data2;
-                        _data_DB->command="SELECT ";
+        _data_DB->command.append(" values('");
+        _data_DB->command.append(buffer1);
+        _data_DB->command.append("', '1@");
+        _data_DB->command.append(buffer1);
+        _data_DB->command.append("@");
+        _data_DB->command.append(buffer);
+        _data_DB->command.append("');");
+
+        rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
+        if((rc != SQLITE_OK) && (rc != 101))
+           WARNING("SQL error: %s\n", errorMsg);
+    }
+
+    int32_t numstatements[3] = {0 ,0 ,0};
+    for(size_t i; i < miss[activeMission].numServices; i++) {
+        if(miss[activeMission].services[i].name.compare("if") == 0) {
+            input.clear();
+            check.clear();
+
+            for(size_t t = 0; t < 10; t++) {
+                if(!miss[activeMission].services[i].output[t].empty()) {
+                    input = miss[activeMission].services[i - numstatements[0] - 1].output[t];
+
+                    _data_DB->command="SELECT ";
                     _data_DB->command.append(_data_DB->tablename);
                     _data_DB->command.append(".* from ");
                     _data_DB->command.append(_data_DB->tablename);
                     _data_DB->command.append(" where Tag=='");
-                    _data_DB->command.append(miss[activeMission].services[k].output[t].erase(0, 1));
+                    _data_DB->command.append(input);
                     _data_DB->command.append("';");
-                    sqlite3_stmt * pStatement;
-                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-                    if (rc == SQLITE_OK){ 
-                    if (sqlite3_step(pStatement) == SQLITE_ROW)
+
+                    sqlite3_stmt *pStatement;
+                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), \
+                            -1, &pStatement, NULL);
+                    if(rc == SQLITE_OK) { 
+                        if(sqlite3_step(pStatement) == SQLITE_ROW)
+                             data = (const char *) sqlite3_column_text(pStatement, 1); 
+                        else {
+                            LOG("1 data_DB:: Data not yet in DB.\n");
+                            rc=31337;
+                        }
+                    } else {
+                        LOG("data_DB:: Error executing SQL statement. rc = %i\n%s\n", \
+                                rc,_data_DB->command.c_str());
+                    }
+
+                    sqlite3_finalize(pStatement);
+                    int32_t pos = data.find_last_of("@", data.length() - 2);
+                    token = data.substr(pos + 1);
+                    token.erase(token.length() - 1);
+                    data.clear();
+                    break;
+                }
+            }
+
+            bool doit = false;
+            if(miss[activeMission].services[i].output[t].find(">") != string::npos) {
+                std::string data2;
+                _data_DB->command="SELECT ";
+                _data_DB->command.append(_data_DB->tablename);
+                _data_DB->command.append(".* from ");
+                _data_DB->command.append(_data_DB->tablename);
+                _data_DB->command.append(" where Tag=='");
+                _data_DB->command.append(miss[activeMission].services[i].output[t].erase(0, 1));
+                _data_DB->command.append("';");
+                sqlite3_stmt *pStatement;
+
+                rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), \
+                        -1, &pStatement, NULL);
+                if(rc == SQLITE_OK) { 
+                    if(sqlite3_step(pStatement) == SQLITE_ROW)
                          data2 = (const char *) sqlite3_column_text(pStatement, 1); 
                     else {
-                        printf("4 data_DB:: Data not yet in DB.\n");
-                        rc=31337;
+                        LOG("2 data_DB:: Data not yet in DB.\n");
+                        rc = 31337;
                     }
-                    } else {
-                    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-                    }
-                        sqlite3_finalize(pStatement);
-                    int pos = data2.find_last_of("@", data2.length()-2);
-                    token2 = data2.substr(pos+1);
-                    token2.erase(token2.length()-1);
-                        //printf("token=%s token2=%s\n", token.c_str(), token2.c_str());
-                        if(atof(token.c_str()) > atof(token2.c_str()))
-                        doit=true;
+                } else {
+                    LOG("data_DB:: Error executing SQL statement. rc = %i\n%s\n", \
+                            rc, _data_DB->command.c_str());
                 }
-                else if (miss[activeMission].services[k].output[t].find(token) != string::npos)
-                doit=true;
-                if(doit){
-                //printf("Level 1:if taken\n");
-                for(uint16_t j = k+1; j <= k+miss[activeMission].services[k].num_conds; j++){ 
-                    if(miss[activeMission].services[j].name.compare("if")==0){
-                    //printf("Level 2:if detected\n");
+
+                sqlite3_finalize(pStatement);
+
+                int32_t pos = data2.find_last_of("@", data2.length() - 2);
+                token2 = data2.substr(pos + 1);
+                token2.erase(token2.length() - 1);
+                if(atof(token.c_str()) > atof(token2.c_str()))
+                    doit = true;
+            }
+            else if(miss[activeMission].services[i].output[t].find(token) != string::npos)
+                doit = true;
+
+            if(doit) {
+                for(size_t k = i + 1; k <= i+miss[activeMission].services[i].num_conds; k++) { 
+                    if(miss[activeMission].services[k].name.compare("if") == 0) {
                         input.clear();
                         check.clear();
-                        for(t = 0; t < 10; t++){
-                        if(!miss[activeMission].services[j].output[t].empty()){
-                            input=miss[activeMission].services[j-numstatements[2]-1].output[t];
-                                   _data_DB->command="SELECT ";
+                        for(size_t t = 0; t < 10; t++) {
+                            if(!miss[activeMission].services[k].output[t].empty()) {
+                                input = miss[activeMission].services[k - numstatements[1] - 1].output[t];
+                                _data_DB->command="SELECT ";
                                 _data_DB->command.append(_data_DB->tablename);
-                               _data_DB->command.append(".* from ");
-                               _data_DB->command.append(_data_DB->tablename);
-                              _data_DB->command.append(" where Tag=='");
-                             _data_DB->command.append(input);
-                            _data_DB->command.append("';");
-                            sqlite3_stmt * pStatement;
-                            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-                            if (rc == SQLITE_OK){ 
-                            if (sqlite3_step(pStatement) == SQLITE_ROW)
-                                 data = (const char *) sqlite3_column_text(pStatement, 1); 
-                            else {
-                                printf("5 data_DB:: Data not yet in DB.\n");
-                                rc=31337;
+                                _data_DB->command.append(".* from ");
+                                _data_DB->command.append(_data_DB->tablename);
+                                _data_DB->command.append(" where Tag=='");
+                                _data_DB->command.append(input);
+                                _data_DB->command.append("';");
+
+                                sqlite3_stmt *pStatement;
+                                rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), \
+                                        -1, &pStatement, NULL);
+                                if(rc == SQLITE_OK) { 
+                                    if(sqlite3_step(pStatement) == SQLITE_ROW)
+                                         data = (const char *) sqlite3_column_text(pStatement, 1); 
+                                    else {
+                                        LOG("3 data_DB:: Data not yet in DB.\n");
+                                        rc = 31337;
+                                    }
+                                } else {
+                                    LOG("data_DB:: Error executing SQL statement. rc = %i\n%s\n", \
+                                            rc,_data_DB->command.c_str());
+                                }
+
+                                sqlite3_finalize(pStatement);
+                                int32_t pos = data.find_last_of("@", data.length() - 2);
+                                token = data.substr(pos + 1);
+                                token.erase(token.length() - 1);
+                                break;
                             }
-                            } else {
-                            printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-                            }
-                                sqlite3_finalize(pStatement);
-                            //printf("data=%s\n", data.c_str());
-                            int pos = data.find_last_of("@", data.length()-2);
-                            token = data.substr(pos+1);
-                            token.erase(token.length()-1);
-                            //printf("token=%s\n", token.c_str());
-                            data.clear();
-                            break;
                         }
-                        }
+
                         bool doit = false;
-                        if(miss[activeMission].services[j].output[t].find(">") != string::npos){
+                        if(miss[activeMission].services[k].output[t].find(">") != string::npos) {
+                            std::string data2;
                             _data_DB->command="SELECT ";
                             _data_DB->command.append(_data_DB->tablename);
                             _data_DB->command.append(".* from ");
-                           _data_DB->command.append(_data_DB->tablename);
+                            _data_DB->command.append(_data_DB->tablename);
                             _data_DB->command.append(" where Tag=='");
-                            _data_DB->command.append(miss[activeMission].services[j].output[t].erase(0, 1));
+                            _data_DB->command.append(miss[activeMission].services[k].output[t].erase(0, 1));
                             _data_DB->command.append("';");
                             sqlite3_stmt * pStatement;
@@ -1119,5 +953,5 @@
                                  data2 = (const char *) sqlite3_column_text(pStatement, 1); 
                             else {
-                                printf("6 data_DB:: Data not yet in DB.\n");
+                                printf("4 data_DB:: Data not yet in DB.\n");
                                 rc=31337;
                             }
@@ -1126,32 +960,170 @@
                             }
                                 sqlite3_finalize(pStatement);
-
-                        int pos = data2.find_last_of("@", data2.length()-2);
-                        token2 = data2.substr(pos+1);
-                        token2.erase(token2.length()-1);
-                            if(atof(token.c_str()) > atof(token2.c_str()))
-                            doit=true;
-                        data.clear();
+                            int pos = data2.find_last_of("@", data2.length()-2);
+                            token2 = data2.substr(pos+1);
+                            token2.erase(token2.length()-1);
+                                //printf("token=%s token2=%s\n", token.c_str(), token2.c_str());
+                                if(atof(token.c_str()) > atof(token2.c_str()))
+                                doit=true;
                         }
-                        else if (miss[activeMission].services[j].output[t].find(token) != string::npos)
+                        else if (miss[activeMission].services[k].output[t].find(token) != string::npos)
                         doit=true;
                         if(doit){
                         //printf("Level 1:if taken\n");
-                        for(uint16_t l = j+1; l <= j+miss[activeMission].services[j].num_conds; l++){ 
-                            TransactData(l);
+                        for(uint16_t j = k+1; j <= k+miss[activeMission].services[k].num_conds; j++){ 
+                            if(miss[activeMission].services[j].name.compare("if")==0){
+                            //printf("Level 2:if detected\n");
+                                input.clear();
+                                check.clear();
+                                for(t = 0; t < 10; t++){
+                                if(!miss[activeMission].services[j].output[t].empty()){
+                                    input=miss[activeMission].services[j-numstatements[2]-1].output[t];
+                                           _data_DB->command="SELECT ";
+                                        _data_DB->command.append(_data_DB->tablename);
+                                       _data_DB->command.append(".* from ");
+                                       _data_DB->command.append(_data_DB->tablename);
+                                      _data_DB->command.append(" where Tag=='");
+                                     _data_DB->command.append(input);
+                                    _data_DB->command.append("';");
+                                    sqlite3_stmt * pStatement;
+                                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
+                                    if (rc == SQLITE_OK){ 
+                                    if (sqlite3_step(pStatement) == SQLITE_ROW)
+                                         data = (const char *) sqlite3_column_text(pStatement, 1); 
+                                    else {
+                                        printf("5 data_DB:: Data not yet in DB.\n");
+                                        rc=31337;
+                                    }
+                                    } else {
+                                    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
+                                    }
+                                        sqlite3_finalize(pStatement);
+                                    //printf("data=%s\n", data.c_str());
+                                    int pos = data.find_last_of("@", data.length()-2);
+                                    token = data.substr(pos+1);
+                                    token.erase(token.length()-1);
+                                    //printf("token=%s\n", token.c_str());
+                                    data.clear();
+                                    break;
+                                }
+                                }
+                                bool doit = false;
+                                if(miss[activeMission].services[j].output[t].find(">") != string::npos){
+                                    _data_DB->command="SELECT ";
+                                    _data_DB->command.append(_data_DB->tablename);
+                                    _data_DB->command.append(".* from ");
+                                   _data_DB->command.append(_data_DB->tablename);
+                                    _data_DB->command.append(" where Tag=='");
+                                    _data_DB->command.append(miss[activeMission].services[j].output[t].erase(0, 1));
+                                    _data_DB->command.append("';");
+                                    sqlite3_stmt * pStatement;
+                                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
+                                    if (rc == SQLITE_OK){ 
+                                    if (sqlite3_step(pStatement) == SQLITE_ROW)
+                                         data2 = (const char *) sqlite3_column_text(pStatement, 1); 
+                                    else {
+                                        printf("6 data_DB:: Data not yet in DB.\n");
+                                        rc=31337;
+                                    }
+                                    } else {
+                                    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
+                                    }
+                                        sqlite3_finalize(pStatement);
+
+                                int pos = data2.find_last_of("@", data2.length()-2);
+                                token2 = data2.substr(pos+1);
+                                token2.erase(token2.length()-1);
+                                    if(atof(token.c_str()) > atof(token2.c_str()))
+                                    doit=true;
+                                data.clear();
+                                }
+                                else if (miss[activeMission].services[j].output[t].find(token) != string::npos)
+                                doit=true;
+                                if(doit){
+                                //printf("Level 1:if taken\n");
+                                for(uint16_t l = j+1; l <= j+miss[activeMission].services[j].num_conds; l++){ 
+                                    TransactData(l);
+                                }
+                                }
+                                else
+                                //printf("Level 2:if not taken\n");
+                                numstatements[2] +=miss[activeMission].services[j].num_conds+1;
+                                j+=miss[activeMission].services[j].num_conds;
+                                //printf("Level 2:doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
+                            }else if(miss[activeMission].services[j].name.compare("dowhile")==0){
+                                numstatements[0]=0;
+                                //printf("Level 2:while detected\n");
+                                while(true){
+                                    uint16_t l;
+                                    for(l = j+1; l <= j+miss[activeMission].services[j].num_conds; l++){ 
+                                    TransactData(l);
+                                    }
+                                    data.clear();
+                                    input.clear();
+                                    check.clear();
+                                    int t;
+                                    for(t = 0; t < 10; t++){
+                                    if(!miss[activeMission].services[j].output[t].empty()){
+                                        input=miss[activeMission].services[l-2].output[t];
+                                        //printf("%s\n",input.c_str());
+                                        _data_DB->command="SELECT ";
+                                        _data_DB->command.append(_data_DB->tablename);
+                                        _data_DB->command.append(".* from ");
+                                        _data_DB->command.append(_data_DB->tablename);
+                                        _data_DB->command.append(" where Tag=='");
+                                        _data_DB->command.append(input);
+                                        _data_DB->command.append("';");
+                                        sqlite3_stmt * pStatement;
+
+                                        rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
+                                        if (rc == SQLITE_OK){ 
+                                        if (sqlite3_step(pStatement) == SQLITE_ROW)
+                                             data = (const char *) sqlite3_column_text(pStatement, 1); 
+                                        else {
+                                            printf("7 data_DB:: Data not yet in DB.: %s\n", _data_DB->command.c_str());
+                                            rc=31337;
+                                        }
+                                        } else {
+                                          printf("data_DB:: SQL statement error. rc = %i\n%s\n",rc,_data_DB->command.c_str());
+                                        }
+                                            sqlite3_finalize(pStatement);
+                                        //printf("Level 2:data=%s\n", data.c_str());
+                                            int pos = data.find_last_of("@", data.length()-2);
+                                            token = data.substr(pos+1);
+                                            token.erase(token.length()-1);
+                                        //printf("Level 2:token=%s\n", token);
+                                        break;
+                                    }
+                                    }
+                                    //printf("Level 2:--- %s  %s---\n", miss[activeMission].services[j].output[t].c_str(), token);
+                                    if(miss[activeMission].services[j].output[t].find(token) != string::npos){
+                                    //printf("Level 2:while taken again!\n");
+                                    }
+                                    else {
+                                    //printf("Level 2:no more while\n");
+                                    break;}
+                                }
+                                j+=miss[activeMission].services[j].num_conds;
+                            }
+                            else{
+                                //printf("Level 2: no conditional\n");
+                                numstatements[2]=0;
+                                    TransactData(j);
+                            }
                         }
                         }
-                        else
-                        //printf("Level 2:if not taken\n");
-                        numstatements[2] +=miss[activeMission].services[j].num_conds+1;
-                        j+=miss[activeMission].services[j].num_conds;
-                        //printf("Level 2:doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
-                    }else if(miss[activeMission].services[j].name.compare("dowhile")==0){
+                        else{
+                        //printf("Level 1: if not taken\n");
+                        }
+                            numstatements[1] +=miss[activeMission].services[k].num_conds+1;
+                            k+=miss[activeMission].services[k].num_conds;
+                        //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
+                    }else if(miss[activeMission].services[k].name.compare("dowhile")==0){
                         numstatements[0]=0;
-                        //printf("Level 2:while detected\n");
+                        //printf("Level 1: while detected\n");
                         while(true){
-                            uint16_t l;
-                            for(l = j+1; l <= j+miss[activeMission].services[j].num_conds; l++){ 
-                            TransactData(l);
+                        uint16_t j;
+                            for(j = k+1; j <= k+miss[activeMission].services[k].num_conds; j++){ 
+                            TransactData(j);
                             }
                             data.clear();
@@ -1160,7 +1132,6 @@
                             int t;
                             for(t = 0; t < 10; t++){
-                            if(!miss[activeMission].services[j].output[t].empty()){
-                                input=miss[activeMission].services[l-2].output[t];
-                                //printf("%s\n",input.c_str());
+                            if(!miss[activeMission].services[k].output[t].empty()){
+                                input=miss[activeMission].services[j-1].output[t];
                                 _data_DB->command="SELECT ";
                                 _data_DB->command.append(_data_DB->tablename);
@@ -1171,5 +1142,4 @@
                                 _data_DB->command.append("';");
                                 sqlite3_stmt * pStatement;
-
                                 rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
                                 if (rc == SQLITE_OK){ 
@@ -1177,164 +1147,99 @@
                                      data = (const char *) sqlite3_column_text(pStatement, 1); 
                                 else {
-                                    printf("7 data_DB:: Data not yet in DB.: %s\n", _data_DB->command.c_str());
+                                    printf("8 data_DB:: Data not yet in DB.\n");
                                     rc=31337;
                                 }
                                 } else {
-                                  printf("data_DB:: SQL statement error. rc = %i\n%s\n",rc,_data_DB->command.c_str());
+                                printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
                                 }
                                     sqlite3_finalize(pStatement);
-                                //printf("Level 2:data=%s\n", data.c_str());
-                                    int pos = data.find_last_of("@", data.length()-2);
-                                    token = data.substr(pos+1);
-                                    token.erase(token.length()-1);
-                                //printf("Level 2:token=%s\n", token);
+                                //printf("Level 1:data=%s\n", data.c_str());
+                                int pos = data.find_last_of("@", data.length()-2);
+                                token = data.substr(pos+1);
+                                token.erase(token.length()-1);
+                                //printf("Level 1:token=%s\n", token);
                                 break;
                             }
                             }
-                            //printf("Level 2:--- %s  %s---\n", miss[activeMission].services[j].output[t].c_str(), token);
-                            if(miss[activeMission].services[j].output[t].find(token) != string::npos){
-                            //printf("Level 2:while taken again!\n");
+                            //printf("Level 1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token);
+                            if(miss[activeMission].services[k].output[t].find(token) != string::npos){
+                            //printf("Level 1:while taken again!\n");
                             }
                             else {
-                            //printf("Level 2:no more while\n");
+                            //printf("Level 1:While finished\n");
                             break;}
                         }
-                        j+=miss[activeMission].services[j].num_conds;
+                        k+=miss[activeMission].services[k].num_conds;
                     }
                     else{
-                        //printf("Level 2: no conditional\n");
-                        numstatements[2]=0;
-                            TransactData(j);
+                        //printf("Level1:No conditional\n");
+                        numstatements[1]=0;
+                            TransactData(k);
                     }
                 }
+            }
+                numstatements[0] +=miss[activeMission].services[i].num_conds+1;
+            i+=miss[activeMission].services[i].num_conds;
+        }
+        else if(miss[activeMission].services[i].name.compare("dowhile")==0)
+        {
+            numstatements[0]=0;
+            //printf("Level 0: while detected\n");
+            while(true){
+            uint16_t k;
+                for(k = i+1; k <= i+miss[activeMission].services[i].num_conds; k++){ 
+                TransactData(k);
                 }
-                else{
-                //printf("Level 1: if not taken\n");
+                data.clear();
+                input.clear();
+                check.clear();
+                int t;
+                for(t = 0; t < 10; t++){
+                if(!miss[activeMission].services[i].output[t].empty()){
+                    input=miss[activeMission].services[k-1].output[t];
+                    _data_DB->command="SELECT ";
+                    _data_DB->command.append(_data_DB->tablename);
+                    _data_DB->command.append(".* from ");
+                    _data_DB->command.append(_data_DB->tablename);
+                    _data_DB->command.append(" where Tag=='");
+                    _data_DB->command.append(input);
+                    _data_DB->command.append("';");
+                    sqlite3_stmt * pStatement;
+                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
+                    if (rc == SQLITE_OK){ 
+                    if (sqlite3_step(pStatement) == SQLITE_ROW)
+                         data = (const char *) sqlite3_column_text(pStatement, 1); 
+                    else {
+                        printf("10data_DB:: Data not yet in DB.\n");
+                        rc=31337;
+                    }
+                    } else {
+                    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
+                    }
+                        sqlite3_finalize(pStatement);
+                    int pos = data.find_last_of("@", data.length()-2);
+                    token = data.substr(pos+1);
+                    token.erase(token.length()-1);
+                    break;
                 }
-                    numstatements[1] +=miss[activeMission].services[k].num_conds+1;
-                    k+=miss[activeMission].services[k].num_conds;
-                //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
-            }else if(miss[activeMission].services[k].name.compare("dowhile")==0){
-                numstatements[0]=0;
-                //printf("Level 1: while detected\n");
-                while(true){
-                uint16_t j;
-                    for(j = k+1; j <= k+miss[activeMission].services[k].num_conds; j++){ 
-                    TransactData(j);
-                    }
-                    data.clear();
-                    input.clear();
-                    check.clear();
-                    int t;
-                    for(t = 0; t < 10; t++){
-                    if(!miss[activeMission].services[k].output[t].empty()){
-                        input=miss[activeMission].services[j-1].output[t];
-                        _data_DB->command="SELECT ";
-                        _data_DB->command.append(_data_DB->tablename);
-                        _data_DB->command.append(".* from ");
-                        _data_DB->command.append(_data_DB->tablename);
-                        _data_DB->command.append(" where Tag=='");
-                        _data_DB->command.append(input);
-                        _data_DB->command.append("';");
-                        sqlite3_stmt * pStatement;
-                        rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-                        if (rc == SQLITE_OK){ 
-                        if (sqlite3_step(pStatement) == SQLITE_ROW)
-                             data = (const char *) sqlite3_column_text(pStatement, 1); 
-                        else {
-                            printf("8 data_DB:: Data not yet in DB.\n");
-                            rc=31337;
-                        }
-                        } else {
-                        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-                        }
-                            sqlite3_finalize(pStatement);
-                        //printf("Level 1:data=%s\n", data.c_str());
-                        int pos = data.find_last_of("@", data.length()-2);
-                        token = data.substr(pos+1);
-                        token.erase(token.length()-1);
-                        //printf("Level 1:token=%s\n", token);
-                        break;
-                    }
-                    }
-                    //printf("Level 1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token);
-                    if(miss[activeMission].services[k].output[t].find(token) != string::npos){
-                    //printf("Level 1:while taken again!\n");
-                    }
-                    else {
-                    //printf("Level 1:While finished\n");
-                    break;}
                 }
-                k+=miss[activeMission].services[k].num_conds;
+                //printf("Level 0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
+                if(miss[activeMission].services[i].output[t].find(token) != string::npos){
+                //printf("Level 0:while taken again!\n");
+                }
+                else {
+                //printf("Level 0:no more while\n");
+                break;}
             }
-            else{
-                //printf("Level1:No conditional\n");
-                numstatements[1]=0;
-                    TransactData(k);
-            }
-        }
-        }
-            numstatements[0] +=miss[activeMission].services[i].num_conds+1;
-        i+=miss[activeMission].services[i].num_conds;
-    }
-    else if(miss[activeMission].services[i].name.compare("dowhile")==0)
-    {
-        numstatements[0]=0;
-        //printf("Level 0: while detected\n");
-        while(true){
-        uint16_t k;
-            for(k = i+1; k <= i+miss[activeMission].services[i].num_conds; k++){ 
-            TransactData(k);
-            }
-            data.clear();
-            input.clear();
-            check.clear();
-            int t;
-            for(t = 0; t < 10; t++){
-            if(!miss[activeMission].services[i].output[t].empty()){
-                input=miss[activeMission].services[k-1].output[t];
-                _data_DB->command="SELECT ";
-                _data_DB->command.append(_data_DB->tablename);
-                _data_DB->command.append(".* from ");
-                _data_DB->command.append(_data_DB->tablename);
-                _data_DB->command.append(" where Tag=='");
-                _data_DB->command.append(input);
-                _data_DB->command.append("';");
-                sqlite3_stmt * pStatement;
-                rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-                if (rc == SQLITE_OK){ 
-                if (sqlite3_step(pStatement) == SQLITE_ROW)
-                     data = (const char *) sqlite3_column_text(pStatement, 1); 
-                else {
-                    printf("10data_DB:: Data not yet in DB.\n");
-                    rc=31337;
-                }
-                } else {
-                printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-                }
-                    sqlite3_finalize(pStatement);
-                int pos = data.find_last_of("@", data.length()-2);
-                token = data.substr(pos+1);
-                token.erase(token.length()-1);
-                break;
-            }
-            }
-            //printf("Level 0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
-            if(miss[activeMission].services[i].output[t].find(token) != string::npos){
-            //printf("Level 0:while taken again!\n");
-            }
-            else {
-            //printf("Level 0:no more while\n");
-            break;}
-        }
-        i+=miss[activeMission].services[i].num_conds;
-    }
-    else{
-        numstatements[0]=0;
-        //printf("Level 0: No conditional\n");
-        TransactData(i);}
-    i++;
-    }
-    i=0;
+            i+=miss[activeMission].services[i].num_conds;
+        }
+        else{
+            numstatements[0]=0;
+            //printf("Level 0: No conditional\n");
+            TransactData(i);
+        }
+    }
+
+    int32_t i = 0;
     data.clear();
 
