Added missing clamping in generate_batches