Index: vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp
===================================================================
--- vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 421)
+++ vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 438)
@@ -139,5 +139,5 @@
 //Also, their sizes are hardcoded into the code in various places; a fix for a future version.
 ServiceManagementLayer::ServiceManagementLayer(const char* SML_Config, \
-	const char* serverName, const char* serverPort, int16_t clientPort)
+    const char* serverName, const char* serverPort, int16_t clientPort)
 {
     LOG("Creating Service Management Layer.\n");
@@ -151,5 +151,5 @@
     miss = new Mission[10];
     for(int i = 0; i < 10; i++)
-    	miss[i].services = new Service[30];
+        miss[i].services = new Service[30];
 
     Current_ID = 0;
@@ -296,5 +296,5 @@
 /* CALLED BY: constructor
  * INPUTS: |serverName| the IPv4 name of the server (127.0.0.1 for localhost)
- *	   |serverPort| the port on the server to connect to
+ *       |serverPort| the port on the server to connect to
  * OUTPUTS: <none>
  *
@@ -321,12 +321,12 @@
 ServiceManagementLayer::MessageHandler(int32_t ID)
 {
-    char buffer[256];	
+    char buffer[256];    
     memset(buffer, 0, 256);  
     int32_t _FD;  
     
     if(ID != -1)
-	_FD = CE_List[ID].FD;
+    _FD = CE_List[ID].FD;
     else 
-	_FD = shellSocketFD;
+    _FD = shellSocketFD;
     ReadMessage(_FD, buffer);
     //printf("MH_buffer = %s\n", buffer);
@@ -371,26 +371,26 @@
     }
     else if(strcmp(buffer, "register_engine_cognitive") == 0) {
-     	RegisterCognitiveEngine(ID);
+         RegisterCognitiveEngine(ID);
     }
     else if(strcmp(buffer, "register_service") == 0) {
- 	ReceiveServices(ID);
+     ReceiveServices(ID);
     }
     else if(strcmp(buffer, "send_component_type") == 0) {
- 	SendComponentType();
+     SendComponentType();
     }
     else if(strcmp(buffer, "list_services") == 0) {
- 	ListServices();
+     ListServices();
     }
     else if(strcmp(buffer, "set_active_mission") == 0) {
-	SetActiveMission();
+    SetActiveMission();
     }
     else if(strcmp(buffer, "request_optimization") == 0) {
-	PerformActiveMission();
+    PerformActiveMission();
     }
     else if(strcmp(buffer, "deregister_engine_cognitive") == 0) {
-	DeregisterCognitiveEngine(ID);
+    DeregisterCognitiveEngine(ID);
     }
     else if(strcmp(buffer, "deregister_service") == 0) {
-	DeregisterServices(ID);
+    DeregisterServices(ID);
     }
 }
@@ -478,9 +478,9 @@
     while(rc!=0){
         memset(buffer, 0, 256);
-	//Receive data from Shell
-	ReadMessage(shellSocketFD, buffer);
-	//printf("buffer = %s\n", buffer);
-	//Send data to CE
-	SendMessage(CE_List[ID].FD, buffer);
+    //Receive data from Shell
+    ReadMessage(shellSocketFD, buffer);
+    //printf("buffer = %s\n", buffer);
+    //Send data to CE
+    SendMessage(CE_List[ID].FD, buffer);
         FD_ZERO(&sockSet);
         FD_SET(shellSocketFD, &sockSet);
@@ -515,11 +515,11 @@
     //Send data until the CE sends an ACK message back
     while(rc!=0){
-	//printf("transfering...\n");
+    //printf("transfering...\n");
         memset(buffer, 0, 256);
-	//Receive data from Shell
-	ReadMessage(shellSocketFD, buffer);
-	//printf("buffer = %s\n", buffer);
-	//Send data to CE
-	SendMessage(CE_List[ID].FD, buffer);
+    //Receive data from Shell
+    ReadMessage(shellSocketFD, buffer);
+    //printf("buffer = %s\n", buffer);
+    //Send data to CE
+    SendMessage(CE_List[ID].FD, buffer);
         FD_ZERO(&sockSet);
         FD_SET(shellSocketFD, &sockSet);
@@ -594,7 +594,7 @@
     for(activeMission = 0; activeMission < 10; activeMission++)
     {
-	//Find the active mission by comparing mission ID's
-	if(miss[activeMission].missionID == missID)
-	    break;
+    //Find the active mission by comparing mission ID's
+    if(miss[activeMission].missionID == missID)
+        break;
     }
 
@@ -602,35 +602,35 @@
     //For each service in the mission
     for(uint16_t i = 0; i < miss[activeMission].numServices; i++)
-    {	
-	//Check whether the current service is an actual service or a conditional
-	if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("dowhile") && \
-	   miss[activeMission].services[i].name.compare("shell")){
-	    //If it is a service, search the database of registered services to find the ID of the component that registered it
-    	    _services_DB->command="select ";
-   	    _services_DB->command.append(_services_DB->tablename);
-    	    _services_DB->command.append(".* from ");
-    	    _services_DB->command.append( _services_DB->tablename);
-    	    _services_DB->command.append(" where Service_Name=='");
-	    _services_DB->command.append(miss[activeMission].services[i].name);
-	    _services_DB->command.append("';");
-	
+    {    
+    //Check whether the current service is an actual service or a conditional
+    if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("dowhile") && \
+       miss[activeMission].services[i].name.compare("shell")){
+        //If it is a service, search the database of registered services to find the ID of the component that registered it
+            _services_DB->command="select ";
+           _services_DB->command.append(_services_DB->tablename);
+            _services_DB->command.append(".* from ");
+            _services_DB->command.append( _services_DB->tablename);
+            _services_DB->command.append(" where Service_Name=='");
+        _services_DB->command.append(miss[activeMission].services[i].name);
+        _services_DB->command.append("';");
+    
             sqlite3_stmt * pStatement;
             int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), -1, &pStatement, NULL);
             if (rc == SQLITE_OK){ 
                 if (sqlite3_step(pStatement) == SQLITE_ROW)
-	             miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0); 
+                 miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0); 
                 else {
-	            printf("services_DB:: Mission requires service %s not provided by any connected component.\n",miss[activeMission].services[i].name.c_str());
-		    rc=31337;
-	        }
+                printf("services_DB:: Mission requires service %s not provided by any connected component.\n",miss[activeMission].services[i].name.c_str());
+            rc=31337;
+            }
              } else {
-	        printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command.c_str());
-            }
-		//printf("s_name=%s\n",miss[activeMission].services[i].name.c_str());
+            printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command.c_str());
+            }
+        //printf("s_name=%s\n",miss[activeMission].services[i].name.c_str());
             sqlite3_finalize(pStatement);
-	    miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD;
-	    //Set the FD and ID of the service to refer to the component where the service exists
-	}
-	//Nothing to be done for conditionals at this stage
+        miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD;
+        //Set the FD and ID of the service to refer to the component where the service exists
+    }
+    //Nothing to be done for conditionals at this stage
     }
  
@@ -663,41 +663,41 @@
    if(miss[activeMission].services[sourceID].name.find("shell")!=string::npos)
    { 
-	int k = 0;
-	shellFound=true;
-	while(k<10 && !miss[activeMission].input[k].empty()){
-	    k++;
-	}
-	sprintf(buffer, "%d", k);
-	SendMessage(shellSocketFD, buffer);
-	for(int t = 0; t < k; t++){
-	    memset(buffer, 0 , 256);
+    int k = 0;
+    shellFound=true;
+    while(k<10 && !miss[activeMission].input[k].empty()){
+        k++;
+    }
+    sprintf(buffer, "%d", k);
+    SendMessage(shellSocketFD, buffer);
+    for(int t = 0; t < k; t++){
+        memset(buffer, 0 , 256);
             _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].input[t]);
-	    _data_DB->command.append("';");
+           _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].input[t]);
+        _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=((const char*) sqlite3_column_text(pStatement, 1)); 
+                data=((const char*) sqlite3_column_text(pStatement, 1)); 
                 else {
-	            printf("3data_DB:: Data not yet in DB., %s\n", _data_DB->command.c_str());
-		    rc=31337;
-	        }
+                printf("3data_DB:: Data not yet in DB., %s\n", _data_DB->command.c_str());
+            rc=31337;
+            }
             } 
-	    else {
-	        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
+        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(), "@");
-	    token = strtok(NULL, "@");
-	    SendMessage(shellSocketFD, token);
-	    token = strtok(NULL, "@");
-	    SendMessage(shellSocketFD, token);
-	}
-	return;
+        token = strtok((char *)data.c_str(), "@");
+        token = strtok(NULL, "@");
+        SendMessage(shellSocketFD, token);
+        token = strtok(NULL, "@");
+        SendMessage(shellSocketFD, token);
+    }
+    return;
    } 
 
@@ -709,8 +709,8 @@
    //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());
+    //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());
     }
 
@@ -719,32 +719,32 @@
     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("';");
+       _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)); 
+             data.append((const char*) sqlite3_column_text(pStatement, 1)); 
             else {
-	            printf("2data_DB:: Data not yet in DB.\n");
-		    rc=31337;
-	    }
+                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());
+    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();
+        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;
@@ -752,41 +752,41 @@
     //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);
+    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++;
+    j++;
         data.clear();
     }
@@ -865,23 +865,23 @@
         memset(buffer1, 0, 256);
         ReadMessage(shellSocketFD, buffer1);
-	_data_DB->command="insert 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="insert 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(") ");
         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);
+    _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);
     }
 
@@ -907,417 +907,417 @@
     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(_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("';");
-				    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;
-					}
-				    } 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 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");
-					    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 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 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;
-			}
-			else{
-			    //printf("Level1:No conditional\n");
-			    numstatements[1]=0;
-		    	    TransactData(k);
-			}
-		}
-	    }
+    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(_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("';");
+                    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;
+                    }
+                    } 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 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");
+                        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 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 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;
+            }
+            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+=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;
@@ -1326,14 +1326,14 @@
     if(!shellFound)
     {
-	int k = 0;
-	while(k<10 && !miss[activeMission].input[k].empty()){
-	    k++;
-	}
-	sprintf(buffer, "%d", k);
-	SendMessage(shellSocketFD, buffer);
-	for(int t = 0; t < k; t++){
-	    SendMessage(shellSocketFD, miss[activeMission].input[t].c_str()); 
-	    SendMessage(shellSocketFD, "0");
-	}
+    int k = 0;
+    while(k<10 && !miss[activeMission].input[k].empty()){
+        k++;
+    }
+    sprintf(buffer, "%d", k);
+    SendMessage(shellSocketFD, buffer);
+    for(int t = 0; t < k; t++){
+        SendMessage(shellSocketFD, miss[activeMission].input[t].c_str()); 
+        SendMessage(shellSocketFD, "0");
+    }
     }
 
@@ -1392,5 +1392,5 @@
     miss = new Mission[10];
     for(int i = 0; i < 10; i++)
-    	miss[i].services = new Service[30];
+        miss[i].services = new Service[30];
     LoadConfiguration(_SML_Config, miss);
 }
@@ -1398,5 +1398,5 @@
 /* CALLED BY: constructor
  * INPUTS: |SML_Config| Address (either relitive or full) of the XML file containing mission data
- * 	   |mList| Mission array to be modified 
+ *        |mList| Mission array to be modified 
  * OUTPUTS: <none>
  *
@@ -1437,126 +1437,126 @@
         pChild0 = pChild0->NextSiblingElement()) 
     {
-	int32_t service_num = 0;
-	uint16_t cond_array[] = {0, 0, 0};
-	
-    	for(pChild1  = (pChild0->FirstChildElement())->FirstChildElement(); pChild1; \
-      	    pChild1  = pChild1->NextSiblingElement()) 
-    	{
-	    int32_t conditional_0 = service_num;
-	    for(pChild2 = pChild1->FirstChildElement(); \
-		pChild2; pChild2 = pChild2->NextSiblingElement())
-	    {
-		service_num++;
-		int32_t conditional_1 = service_num;
-		for(pChild3 = pChild2->FirstChildElement(); \
-		    pChild3; pChild3 = pChild3->NextSiblingElement())
-		{
-		    service_num++;
-		    int32_t conditional_2 = service_num;
-			for(pChild4 = pChild3->FirstChildElement(); \
-			    pChild4; pChild4 = pChild4->NextSiblingElement())
-			{
-		   	    service_num++;
-			    if(pChild4->Attribute("name"))
-			        mList[mission_num].services[service_num].name = pChild4->Attribute("name");
-			    else
-			        mList[mission_num].services[service_num].name = pChild4->Value();
-			    for(int i = 1; i <= 10; i++) {
-			        char buffer[9]="input";
-			        sprintf(buffer, "%s%d", buffer, i); 
-			        //printf("buffer=%s\n", buffer);
-			        if(pChild4->Attribute(buffer))
-			    	    mList[mission_num].services[service_num].input[i-1] = pChild4->Attribute(buffer);
-			        char buffer2[9]="output";
-			        sprintf(buffer2, "%s%d", buffer2, i); 
-			        if(pChild4->Attribute(buffer2))
-				    mList[mission_num].services[service_num].output[i-1] = pChild4->Attribute(buffer2);
-			    }
-			    if(pChild4->Attribute("parameter"))
-				mList[mission_num].services[service_num].parameter = pChild4->Attribute("parameter");
-			    cond_array[2]++;
-			}
-			if(!strcmp(pChild3->Value(), "shell") || conditional_2 != service_num) {
-			    mList[mission_num].services[conditional_2].name = pChild3->Value();
-			}
-			else{
-			    mList[mission_num].services[service_num].name = pChild3->Attribute("name");
-			}
-			for(int i = 1; i <= 10; i++) {
-			    char buffer[9]="input";
-			    sprintf(buffer, "%s%d", buffer, i); 
-			    if(pChild3->Attribute(buffer))
-				mList[mission_num].services[conditional_2].input[i-1] = pChild3->Attribute(buffer);
-			    char buffer2[9]="output";
-			    sprintf(buffer2, "%s%d", buffer2, i); 
-			    if(pChild3->Attribute(buffer2))
-				mList[mission_num].services[conditional_2].output[i-1] = pChild3->Attribute(buffer2);
-			}
-			    if(pChild3->Attribute("parameter"))
-				mList[mission_num].services[conditional_2].parameter = pChild3->Attribute("parameter");
-			mList[mission_num].services[conditional_2].num_conds = cond_array[2];
-			cond_array[1]+=cond_array[2]+1;
-			cond_array[2] = 0;
-
-
-		}
-		if(!strcmp(pChild2->Value(), "shell") || conditional_1 != service_num) {
-		    mList[mission_num].services[conditional_1].name = pChild2->Value();
-		}
-		else{
-		    mList[mission_num].services[service_num].name = pChild2->Attribute("name");
-		}
-   		for(int i = 1; i <= 10; i++) {
-		    char buffer[9]="input";
-		    sprintf(buffer, "%s%d", buffer, i); 
-		    if(pChild2->Attribute(buffer))
-		        mList[mission_num].services[conditional_1].input[i-1] = pChild2->Attribute(buffer);
-		    char buffer2[9]="output";
-		    sprintf(buffer2, "%s%d", buffer2, i); 
-		    if(pChild2->Attribute(buffer2))
-			mList[mission_num].services[conditional_1].output[i-1] = pChild2->Attribute(buffer2);
-		}
-		    if(pChild2->Attribute("parameter"))
-			mList[mission_num].services[conditional_1].parameter = pChild2->Attribute("parameter");
-
-		mList[mission_num].services[conditional_1].num_conds = cond_array[1];
-		cond_array[0]+=cond_array[1]+1;
-		cond_array[1] = 0;
-	    }
-	    
-	    if(!strcmp(pChild1->Value(), "shell") || conditional_0 != service_num) {
-	        mList[mission_num].services[conditional_0].name = pChild1->Value();
-	    }
-	    else{
-		mList[mission_num].services[conditional_0].name = pChild1->Attribute("name");
-	    }
-		for(int i = 1; i <= 10; i++) {
-		    char buffer[9]="input";
-		    sprintf(buffer, "%s%d", buffer, i); 
-		    if(pChild1->Attribute(buffer))
-		        mList[mission_num].services[conditional_0].input[i-1] = pChild1->Attribute(buffer);
-		    char buffer2[9]="output";
-		    sprintf(buffer2, "%s%d", buffer2, i); 
-		    if(pChild1->Attribute(buffer2))
-			mList[mission_num].services[conditional_0].output[i-1] = pChild1->Attribute(buffer2);
-		}
-		if(pChild1->Attribute("parameter"))
-		    mList[mission_num].services[conditional_0].parameter = pChild1->Attribute("parameter");
-	    mList[mission_num].services[conditional_0].num_conds = cond_array[0];
+    int32_t service_num = 0;
+    uint16_t cond_array[] = {0, 0, 0};
+    
+        for(pChild1  = (pChild0->FirstChildElement())->FirstChildElement(); pChild1; \
+              pChild1  = pChild1->NextSiblingElement()) 
+        {
+        int32_t conditional_0 = service_num;
+        for(pChild2 = pChild1->FirstChildElement(); \
+        pChild2; pChild2 = pChild2->NextSiblingElement())
+        {
+        service_num++;
+        int32_t conditional_1 = service_num;
+        for(pChild3 = pChild2->FirstChildElement(); \
+            pChild3; pChild3 = pChild3->NextSiblingElement())
+        {
+            service_num++;
+            int32_t conditional_2 = service_num;
+            for(pChild4 = pChild3->FirstChildElement(); \
+                pChild4; pChild4 = pChild4->NextSiblingElement())
+            {
+                   service_num++;
+                if(pChild4->Attribute("name"))
+                    mList[mission_num].services[service_num].name = pChild4->Attribute("name");
+                else
+                    mList[mission_num].services[service_num].name = pChild4->Value();
+                for(int i = 1; i <= 10; i++) {
+                    char buffer[9]="input";
+                    sprintf(buffer, "%s%d", buffer, i); 
+                    //printf("buffer=%s\n", buffer);
+                    if(pChild4->Attribute(buffer))
+                        mList[mission_num].services[service_num].input[i-1] = pChild4->Attribute(buffer);
+                    char buffer2[9]="output";
+                    sprintf(buffer2, "%s%d", buffer2, i); 
+                    if(pChild4->Attribute(buffer2))
+                    mList[mission_num].services[service_num].output[i-1] = pChild4->Attribute(buffer2);
+                }
+                if(pChild4->Attribute("parameter"))
+                mList[mission_num].services[service_num].parameter = pChild4->Attribute("parameter");
+                cond_array[2]++;
+            }
+            if(!strcmp(pChild3->Value(), "shell") || conditional_2 != service_num) {
+                mList[mission_num].services[conditional_2].name = pChild3->Value();
+            }
+            else{
+                mList[mission_num].services[service_num].name = pChild3->Attribute("name");
+            }
+            for(int i = 1; i <= 10; i++) {
+                char buffer[9]="input";
+                sprintf(buffer, "%s%d", buffer, i); 
+                if(pChild3->Attribute(buffer))
+                mList[mission_num].services[conditional_2].input[i-1] = pChild3->Attribute(buffer);
+                char buffer2[9]="output";
+                sprintf(buffer2, "%s%d", buffer2, i); 
+                if(pChild3->Attribute(buffer2))
+                mList[mission_num].services[conditional_2].output[i-1] = pChild3->Attribute(buffer2);
+            }
+                if(pChild3->Attribute("parameter"))
+                mList[mission_num].services[conditional_2].parameter = pChild3->Attribute("parameter");
+            mList[mission_num].services[conditional_2].num_conds = cond_array[2];
+            cond_array[1]+=cond_array[2]+1;
+            cond_array[2] = 0;
+
+
+        }
+        if(!strcmp(pChild2->Value(), "shell") || conditional_1 != service_num) {
+            mList[mission_num].services[conditional_1].name = pChild2->Value();
+        }
+        else{
+            mList[mission_num].services[service_num].name = pChild2->Attribute("name");
+        }
+           for(int i = 1; i <= 10; i++) {
+            char buffer[9]="input";
+            sprintf(buffer, "%s%d", buffer, i); 
+            if(pChild2->Attribute(buffer))
+                mList[mission_num].services[conditional_1].input[i-1] = pChild2->Attribute(buffer);
+            char buffer2[9]="output";
+            sprintf(buffer2, "%s%d", buffer2, i); 
+            if(pChild2->Attribute(buffer2))
+            mList[mission_num].services[conditional_1].output[i-1] = pChild2->Attribute(buffer2);
+        }
+            if(pChild2->Attribute("parameter"))
+            mList[mission_num].services[conditional_1].parameter = pChild2->Attribute("parameter");
+
+        mList[mission_num].services[conditional_1].num_conds = cond_array[1];
+        cond_array[0]+=cond_array[1]+1;
+        cond_array[1] = 0;
+        }
+        
+        if(!strcmp(pChild1->Value(), "shell") || conditional_0 != service_num) {
+            mList[mission_num].services[conditional_0].name = pChild1->Value();
+        }
+        else{
+        mList[mission_num].services[conditional_0].name = pChild1->Attribute("name");
+        }
+        for(int i = 1; i <= 10; i++) {
+            char buffer[9]="input";
+            sprintf(buffer, "%s%d", buffer, i); 
+            if(pChild1->Attribute(buffer))
+                mList[mission_num].services[conditional_0].input[i-1] = pChild1->Attribute(buffer);
+            char buffer2[9]="output";
+            sprintf(buffer2, "%s%d", buffer2, i); 
+            if(pChild1->Attribute(buffer2))
+            mList[mission_num].services[conditional_0].output[i-1] = pChild1->Attribute(buffer2);
+        }
+        if(pChild1->Attribute("parameter"))
+            mList[mission_num].services[conditional_0].parameter = pChild1->Attribute("parameter");
+        mList[mission_num].services[conditional_0].num_conds = cond_array[0];
             cond_array[0] = 0;
-	    service_num++;
-	}
-	
-	mList[mission_num].numServices = service_num;
-	mList[mission_num].name = pChild0->Attribute("name");
+        service_num++;
+    }
+    
+    mList[mission_num].numServices = service_num;
+    mList[mission_num].name = pChild0->Attribute("name");
         mList[mission_num].missionID = atoi(pChild0->Attribute("id"));
-	for(int i = 1; i <= 10; i++) {
-	    char buffer[9]="param";
-	    sprintf(buffer, "%s%d", buffer, i); 
-	    if(pChild0->Attribute(buffer)){
-	        mList[mission_num].input[i-1] = pChild0->Attribute(buffer);
-	    }
-	}
-	mission_num++;
+    for(int i = 1; i <= 10; i++) {
+        char buffer[9]="param";
+        sprintf(buffer, "%s%d", buffer, i); 
+        if(pChild0->Attribute(buffer)){
+            mList[mission_num].input[i-1] = pChild0->Attribute(buffer);
+        }
+    }
+    mission_num++;
     }
     LOG("ServiceManagementLayer:: Done Loading Configuration\n");
@@ -1644,5 +1644,5 @@
     SendMessage(CE_List[ID].FD, buffer);
     if(strcmp("deregister_ack", buffer) != 0) {
-	ERROR(1, "SML:: Failed to close CE socket\n");
+    ERROR(1, "SML:: Failed to close CE socket\n");
     }
 
@@ -1685,5 +1685,5 @@
     int32_t port, rc, new_sd = 1;
     int32_t desc_ready = 1;
-		//If there is, call the MessageHandler with the Shell_Msg code of -1
+        //If there is, call the MessageHandler with the Shell_Msg code of -1
     fd_set sockSet, shellSet;
 
@@ -1702,8 +1702,8 @@
         
         for(uint16_t k = 0; k < Current_ID; k++){
-	    if(CE_List[k].ID_num != -1)
-	        FD_SET(CE_List[k].FD, &sockSet);
-	}
-	    //printf("k=%d, CID=%d\n", k, CE_List[k].FD);
+        if(CE_List[k].ID_num != -1)
+            FD_SET(CE_List[k].FD, &sockSet);
+    }
+        //printf("k=%d, CID=%d\n", k, CE_List[k].FD);
 
         /* Timeout specification */
@@ -1711,13 +1711,13 @@
         selTimeout.tv_sec = 0;       /* timeout (secs.) */
         selTimeout.tv_usec = 0;            /* 0 microseconds */
-	//Changed both to zero so that select will check messages from the shell instead of blocking
-	//when there is no command from the CE's to be processed
+    //Changed both to zero so that select will check messages from the shell instead of blocking
+    //when there is no command from the CE's to be processed
 
         //Check if there is a message on the socket waiting to be read
-	rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); 
+    rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); 
         //printf("rc=%d\n", rc);
         if(rc == 0){
             //LOG("No echo requests for %i secs...Server still alive\n", 10);
-	
+    
             FD_ZERO(&shellSet);
             FD_SET(shellSocketFD, &shellSet);
@@ -1727,9 +1727,9 @@
             select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout);
             //printf("rc2=%d\n", rc2);
-		//If there is, call the MessageHandler with the Shell_Msg code of -1
-	    if(FD_ISSET(shellSocketFD, &shellSet)){
-		//printf("shell_msg, %d\n", rc2);
-	        MessageHandler(-1);}
-	}
+        //If there is, call the MessageHandler with the Shell_Msg code of -1
+        if(FD_ISSET(shellSocketFD, &shellSet)){
+        //printf("shell_msg, %d\n", rc2);
+            MessageHandler(-1);}
+    }
         else { 
             desc_ready = rc;
@@ -1740,30 +1740,30 @@
                     //Check if request is new or on an existing open descriptor
                     if(port == cogEngSrv) { 
-			//If new, assign it a descriptor and give it an ID
+            //If new, assign it a descriptor and give it an ID
                         new_sd = AcceptTCPConnection(port);
-			 
+             
                         if(new_sd < 0) 
                             break;
 
                         CE_List[Current_ID].FD = new_sd;
-			CE_List[Current_ID].ID_num = Current_ID;
+            CE_List[Current_ID].ID_num = Current_ID;
                         MessageHandler(Current_ID);
-			Current_ID++;
-	
-			FD_SET(new_sd,&sockSet);
+            Current_ID++;
+    
+            FD_SET(new_sd,&sockSet);
                         if(new_sd > maxDescriptor) 
                            maxDescriptor = new_sd;
                     } 
                     else {
-			//If old, figure out which ID it coresponds to and handle it accordingly
-			for(uint16_t z = 0; z < Current_ID; z++)
-			{
-				if(CE_List[z].FD == port){
-					MessageHandler(z);}
-			}
+            //If old, figure out which ID it coresponds to and handle it accordingly
+            for(uint16_t z = 0; z < Current_ID; z++)
+            {
+                if(CE_List[z].FD == port){
+                    MessageHandler(z);}
+            }
                     }
                 }
             }
-	}
+    }
     }        
 
