diff --git a/src/encode.c b/src/encode.c
index 77043895ef9ec9fbcb31a4f5db78d3d9a0dd81d4..34dafa48a6266f5ca323502b9d75104b878f0361 100644
--- a/src/encode.c
+++ b/src/encode.c
@@ -478,11 +478,10 @@ static int m_flush_block(struct aec_stream *strm)
     struct internal_state *state = strm->state;
 
 #ifdef ENABLE_RSI_PADDING
-    if (state->blocks_avail == 0
-        && strm->flags & AEC_PAD_RSI
-        && state->block_nonzero == 0
-        )
-        emit(state, 0, state->bits % 8);
+    if (state->blocks_avail == 0 &&
+        strm->flags & AEC_PAD_RSI &&
+        state->block_nonzero == 0)
+            emit(state, 0, state->bits % 8);
 #endif
 
     if (state->direct_out) {
@@ -490,6 +489,14 @@ static int m_flush_block(struct aec_stream *strm)
         strm->next_out += n;
         strm->avail_out -= n;
         state->mode = m_get_block;
+
+        if (state->ready_to_capture_rsi &&
+            state->blocks_avail == 0 &&
+            state->offsets != NULL) {
+                vector_push_back(state->offsets, (strm->total_out - strm->avail_out) * 8 + (8 - state->bits));
+                state->ready_to_capture_rsi = 0;
+        }
+
         return M_CONTINUE;
     }
 
@@ -711,9 +718,7 @@ static int m_get_block(struct aec_stream *strm)
         state->block = state->data_pp;
         state->blocks_dispensed = 1;
         if (strm->avail_in >= state->rsi_len) {
-            if (state->offsets != NULL)
-                vector_push_back(state->offsets, (strm->total_out - strm->avail_out) * 8 + (8 - state->bits));
-
+            state->ready_to_capture_rsi = 1;
             state->get_rsi(strm);
             if (strm->flags & AEC_DATA_PREPROCESS)
                 state->preprocess(strm);
@@ -885,8 +890,8 @@ int aec_encode_init(struct aec_stream *strm)
     *state->cds = 0;
     state->bits = 8;
     state->mode = m_get_block;
-
     struct vector_t *offsets = NULL;
+    state->ready_to_capture_rsi = 0;
     return AEC_OK;
 }
 
@@ -966,6 +971,7 @@ int aec_encode_enable_offsets(struct aec_stream *strm)
         return AEC_RSI_OFFSETS_ERROR;
 
     state->offsets = vector_create();
+    vector_push_back(state->offsets, 0);
     return AEC_OK;
 }
 
diff --git a/src/encode.h b/src/encode.h
index 940a562b6f4c75d77a031f67a4c46ce11075764f..d2ebca45b0912218b3f3f95f10d8a081aba3a157 100644
--- a/src/encode.h
+++ b/src/encode.h
@@ -142,7 +142,11 @@ struct internal_state {
     /* length of uncompressed CDS */
     uint32_t uncomp_len;
 
+    /* RSI offsets container */
     struct vector_t *offsets;
+
+    /* indicator if an RSI should be captured */
+    int ready_to_capture_rsi;
 };
 
 #endif /* ENCODE_H */
diff --git a/tests/check_rsi_block_access.c b/tests/check_rsi_block_access.c
index b6afea7c8b9fc7c31bf8eba94118b679446b38b0..c12cecb5928c07e86f571e4fef189da559e12136 100644
--- a/tests/check_rsi_block_access.c
+++ b/tests/check_rsi_block_access.c
@@ -1,5 +1,4 @@
 #include "check_aec.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -399,11 +398,19 @@ int test_offsets(struct aec_context *ctx)
     size_t *encode_offsets_ptr;
     size_t encode_offsets_size;
     PREPARE_ENCODE_WITH_OFFSETS(&strm1, ctx, flags, encode_offsets_ptr, &encode_offsets_size);
+    assert(encode_offsets_size > 0);
 
     struct aec_stream strm2;
     size_t *decode_offsets_ptr;
     size_t decode_offsets_size;
     PREPARE_DECODE_WITH_OFFSETS(&strm2, ctx, flags, decode_offsets_ptr, &decode_offsets_size);
+    assert(decode_offsets_size > 0);
+
+    if (encode_offsets_size != decode_offsets_size) {
+        fprintf(stderr, "Error: encode_offsets_size = %zu, decode_offsets_size = %zu\n", encode_offsets_size, decode_offsets_size);
+        return 102;
+    }
+
     size_t size = decode_offsets_size > 10 ? 10 : decode_offsets_size;
 
     for (size_t i = 0; i < encode_offsets_size; ++i) {
@@ -421,10 +428,10 @@ int test_offsets(struct aec_context *ctx)
 int main(void)
 {
     int status    = AEC_OK;
-    size_t ns[]   = {1, 255, 256, 255*10, 256*10, 67000};
-    size_t rsis[] = {1, 2, 255, 256, 512, 1024, 4095, 4096};
-    size_t bss[]  = {8, 16, 32, 64};
-    size_t bpss[] = {1, 7, 8, 9, 15, 16, 17, 23, 24, 25, 31, 32};
+    size_t ns[]   = {1, 255, 256, 255*10, 256*10, 67000};  // number of samples
+    size_t rsis[] = {1, 2, 255, 256, 512, 1024, 4095, 4096};  // RSI size
+    size_t bss[]  = {8, 16, 32, 64};  // block size
+    size_t bpss[] = {1, 7, 8, 9, 15, 16, 17, 23, 24, 25, 31, 32};  // bits per sample
     data_generator_t data_generators[] = {data_generator_zero, data_generator_random, data_generator_incr};
 
     for (size_t n_i = 0; n_i < sizeof(ns) / sizeof(ns[0]); ++n_i) {