updated encodeKV with a byte.Buffer instead of []byte