Replace copyChunk and copyAll with while function.