diff --git a/ADbasic_program_buffered.bas b/ADbasic_program_buffered.bas
index 5baccdd0c61eedf01532a082c8b0b703ffe315dc..d8ef405e3f88e9fbdf59348d845e7f1e79780db1 100644
--- a/ADbasic_program_buffered.bas
+++ b/ADbasic_program_buffered.bas
@@ -54,6 +54,7 @@ DIM DATA_7[AINNO] AS LONG               'Start times for channel recording
 DIM DATA_8[AINNO] AS LONG               'End times for channel recording
 DIM DATA_9[AINNO] AS LONG               'Gain modes for AIN channels
 DIM DATA_199[A_IN_BUFFER+16] AS LONG    'Recorded Data - add padding at the end to ensure no overflow happens
+DIM ain_index[AINNO] AS LONG            'Index of the data acquisition for each channel, to have the values sorted
 
 'Declare arrays for digital data from experiment control software
 DIM DATA_10[MAX_TICO_EVENTS] AS LONG    'DIO1 TiCo times
@@ -89,7 +90,6 @@ DIM pid_prev_dError[AOUTNO] AS FLOAT
 DIM processIdx AS LONG
 DIM eventIdx AS LONG
 DIM pidIdx AS LONG
-DIM aInIdx AS LONG
 DIM nextPidTime AS LONG
 DIM nextAOutTime AS LONG
 DIM timer AS LONG 'timing the event duration
@@ -115,7 +115,6 @@ init:
   processIdx = 0
   eventIdx = 0
   pidIdx = 0
-  aInIdx = 0
   total_workload = 0
   nextPidTime = DATA_4[1]
   nextAOutTime = DATA_1[1]
@@ -165,8 +164,8 @@ init:
   NEXT i
 
   'Write analog out values
-  P2_Write_DAC8(AOUT1,set_output,1)
-  P2_Write_DAC8(AOUT2,set_output,9)
+  P2_Write_DAC8(AOUT1,set_target,1)
+  P2_Write_DAC8(AOUT2,set_target,9)
   'Output set voltages
   P2_Start_DAC(AOUT1)
   P2_Start_DAC(AOUT2)
@@ -198,6 +197,12 @@ init:
     P2_Set_Gain(AIN1,i,DATA_9[i])
     P2_Set_Gain(AIN2,i,DATA_9[i+8])
   NEXT i
+  
+  'Determine indices for analog data acquisition
+  ain_index[1] = 1
+  FOR i=2 TO AINNO
+    ain_index[i] = ain_index[i-1] + DATA_8[i-1] - DATA_7[i-1]
+  NEXT i
 
 
   '### Set the Sampling Rate of the AIN cards. The Period is given by T = 250ns + (value-1)* 10ns. The value is set by writing to the correct register using POKE(register,value)
@@ -217,9 +222,9 @@ init:
   P2_DIGOUT_LONG(DIO2,DATA_21[1])
 
 
-'=========================================================================
-'=                               EVENT LOOP                              =
-'=========================================================================
+  '=========================================================================
+  '=                               EVENT LOOP                              =
+  '=========================================================================
 
 EVENT:
   timer = Read_Timer_Sync()
@@ -277,22 +282,19 @@ EVENT:
   'Set all voltages synchronously
   P2_Sync_All(110000b)
 
-  inc processIdx
-
-
   '======================= SAVE MEASURED AIN VALUES ======================
   ' Write ADC Values
-  IF (aInIdx < A_IN_BUFFER) THEN
-    FOR i=1 to AINNO
-      IF(processIdx <= DATA_8[i]) THEN
-        IF (processIdx > DATA_7[i]) THEN
-          inc aInIdx
-          DATA_199[aInIdx]=act_values[i]
-        ENDIF 'processIdx > DATA_7[i]
-      ENDIF 'processIdx <= DATA_8[i]
-    NEXT i
-  ENDIF 'aInIdx < A_IN_BUFFER
-
+  ' TODO: Array length is checked in labscript, do we still need that here?
+  FOR i=1 to AINNO
+    IF(processIdx <= DATA_8[i]) THEN
+      IF (processIdx > DATA_7[i]) THEN
+        DATA_199[ain_index[i]]=act_values[i]
+        inc ain_index[i]
+      ENDIF 'processIdx > DATA_7[i]
+    ENDIF 'processIdx <= DATA_8[i]
+  NEXT i
+  
+  inc processIdx
 
   '=========================== CHECK END OF RUN ==========================
   IF (processIdx >= par_2) THEN