   p_driftFactor float
 )
     MODIFIES SQL DATA
-    COMMENT 'Inserts into or updates a row in the image quality indicators ta'
-BEGIN
-  DECLARE iqiId int(11) unsigned DEFAULT NULL;
+    COMMENT 'Inserts into or updates a row in the image quality indicators table'
+BEGIN
+  DECLARE row_DataCollectionId int(11) unsigned DEFAULT NULL;
+  DECLARE row_imageNumber mediumint(8) unsigned DEFAULT NULL;
+  
   IF (p_dataCollectionId IS NOT NULL AND p_imageNumber IS NOT NULL) THEN
-    SELECT MAX(imageQualityIndicatorsId) INTO iqiId FROM ImageQualityIndicators WHERE dataCollectionId = p_dataCollectionId AND imageNumber = p_imageNumber;
-    IF iqiId IS NULL THEN
+    SELECT dataCollectionId, imageNumber INTO row_DataCollectionId, row_imageNumber FROM ImageQualityIndicators WHERE dataCollectionId = p_dataCollectionId AND imageNumber = p_imageNumber;
+    IF row_DataCollectionId IS NULL THEN
         INSERT INTO ImageQualityIndicators (
           dataCollectionId, imageNumber, spotTotal, goodBraggCandidates,  
 	      method1Res, method2Res, totalIntegratedSignal, dozor_score, driftFactor) 

/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES' */ ;
DELIMITER ;;
CREATE FUNCTION `root_replace`(p_str varchar(255), p_oldroot varchar(255), p_newroot varchar(255)) RETURNS varchar(255) CHARSET latin1
BEGIN
 DECLARE path_len smallint unsigned DEFAULT LENGTH(p_oldroot);
 RETURN CASE WHEN LEFT(p_str, path_len) = BINARY p_oldroot THEN CONCAT(p_newroot, SUBSTRING(p_str, path_len + 1)) ELSE p_str END; 

/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = '' */ ;
DELIMITER ;;
CREATE PROCEDURE `update_session_paths`(
  p_proposalCode varchar(3),
