optimized code, handle chunked data on the fly