Fixed array out of bound exception

Just added min/max for chunks.