Add encodeChunks, improve decodeChunks