Fix scope issue in read_next_chunk