porting chunk generate function