Updated the loop for finding the end of chunk