1diff --git a/EbmlBufferWriter.c b/EbmlBufferWriter.c
2index 574e478..8c26e80 100644
3--- a/EbmlBufferWriter.c
4+++ b/EbmlBufferWriter.c
5@@ -8,6 +8,31 @@
6 #include <wchar.h>
7 #include <string.h>
8
9+void
10+Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len)
11+{
12+  /* buffer_size:
13+   * 1 - int8_t;
14+   * 2 - int16_t;
15+   * 3 - int32_t;
16+   * 4 - int64_t;
17+   */
18+  long i;
19+  for(i = len-1; i >= 0; i--) {
20+    unsigned char x;
21+    if (buffer_size == 1) {
22+      x = (char)(*(const int8_t *)buffer_in >> (i * 8));
23+	} else if (buffer_size == 2) {
24+      x = (char)(*(const int16_t *)buffer_in >> (i * 8));
25+	} else if (buffer_size == 4) {
26+      x = (char)(*(const int32_t *)buffer_in >> (i * 8));
27+	} else if (buffer_size == 8) {
28+      x = (char)(*(const int64_t *)buffer_in >> (i * 8));
29+	}
30+    Ebml_Write(glob, &x, 1);
31+  }
32+}
33+
34 void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
35   unsigned char *src = glob->buf;
36   src += glob->offset;
37@@ -19,12 +44,12 @@ static void _Serialize(EbmlGlobal *glob, const unsigned char *p, const unsigned
38   while (q != p) {
39     --q;
40
41-    unsigned long cbWritten;
42     memcpy(&(glob->buf[glob->offset]), q, 1);
43     glob->offset++;
44   }
45 }
46
47+/*
48 void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
49   // assert(buf);
50
51@@ -33,22 +58,22 @@ void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len)
52
53   _Serialize(glob, p, q);
54 }
55-
56+*/
57
58 void Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc, unsigned long class_id) {
59+  unsigned long long unknownLen = 0x01FFFFFFFFFFFFFFLL;
60   Ebml_WriteID(glob, class_id);
61   ebmlLoc->offset = glob->offset;
62   // todo this is always taking 8 bytes, this may need later optimization
63-  unsigned long long unknownLen =  0x01FFFFFFFFFFFFFFLLU;
64-  Ebml_Serialize(glob, (void *)&unknownLen, 8); // this is a key that says lenght unknown
65+  Ebml_Serialize(glob, (void *)&unknownLen,sizeof(unknownLen), 8); // this is a key that says lenght unknown
66 }
67
68 void Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) {
69   unsigned long long size = glob->offset - ebmlLoc->offset - 8;
70   unsigned long long curOffset = glob->offset;
71   glob->offset = ebmlLoc->offset;
72-  size |=  0x0100000000000000LLU;
73-  Ebml_Serialize(glob, &size, 8);
74+  size |=  0x0100000000000000LL;
75+  Ebml_Serialize(glob, &size,sizeof(size), 8);
76   glob->offset = curOffset;
77 }
78
79diff --git a/EbmlBufferWriter.h b/EbmlBufferWriter.h
80index acd5c2a..c135f29 100644
81--- a/EbmlBufferWriter.h
82+++ b/EbmlBufferWriter.h
83@@ -11,9 +11,7 @@ typedef struct {
84   unsigned int offset;
85 } EbmlGlobal;
86
87-
88 void Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc, unsigned long class_id);
89 void Ebml_EndSubElement(EbmlGlobal *glob,  EbmlLoc *ebmlLoc);
90
91-
92 #endif
93diff --git a/EbmlWriter.c b/EbmlWriter.c
94index 27cfe86..ebefc1a 100644
95--- a/EbmlWriter.c
96+++ b/EbmlWriter.c
97@@ -74,6 +74,13 @@ void Ebml_WriteID(EbmlGlobal *glob, unsigned long class_id) {
98   Ebml_Serialize(glob, (void *)&class_id, sizeof(class_id), len);
99 }
100
101+void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint32_t ui) {
102+  unsigned char sizeSerialized = 8 | 0x80;
103+  Ebml_WriteID(glob, class_id);
104+  Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
105+  Ebml_Serialize(glob, &ui, sizeof(ui), 4);
106+}
107+
108 void Ebml_SerializeUnsigned64(EbmlGlobal *glob, unsigned long class_id, uint64_t ui) {
109   unsigned char sizeSerialized = 8 | 0x80;
110   Ebml_WriteID(glob, class_id);
111diff --git a/EbmlWriter.h b/EbmlWriter.h
112index b94f757..a0a848b 100644
113--- a/EbmlWriter.h
114+++ b/EbmlWriter.h
115@@ -28,6 +28,7 @@ void Ebml_WriteLen(EbmlGlobal *glob, int64_t val);
116 void Ebml_WriteString(EbmlGlobal *glob, const char *str);
117 void Ebml_WriteUTF8(EbmlGlobal *glob, const wchar_t *wstr);
118 void Ebml_WriteID(EbmlGlobal *glob, unsigned long class_id);
119+void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint32_t ui);
120 void Ebml_SerializeUnsigned64(EbmlGlobal *glob, unsigned long class_id, uint64_t ui);
121 void Ebml_SerializeUnsigned(EbmlGlobal *glob, unsigned long class_id, unsigned long ui);
122 void Ebml_SerializeBinary(EbmlGlobal *glob, unsigned long class_id, unsigned long ui);
123diff --git a/WebMElement.c b/WebMElement.c
124index 2f79a3c..02eefa4 100644
125--- a/WebMElement.c
126+++ b/WebMElement.c
127@@ -11,8 +11,12 @@
128 #include "EbmlIDs.h"
129 #include "WebMElement.h"
130 #include <stdio.h>
131+#include <stdint.h>
132+#include <stdlib.h>
133+#include <time.h>
134
135 #define kVorbisPrivateMaxSize  4000
136+#define UInt64 uint64_t
137
138 void writeHeader(EbmlGlobal *glob) {
139   EbmlLoc start;
140@@ -30,15 +34,16 @@ void writeHeader(EbmlGlobal *glob) {
141 void writeSimpleBlock(EbmlGlobal *glob, unsigned char trackNumber, short timeCode,
142                       int isKeyframe, unsigned char lacingFlag, int discardable,
143                       unsigned char *data, unsigned long dataLength) {
144-  Ebml_WriteID(glob, SimpleBlock);
145   unsigned long blockLength = 4 + dataLength;
146+  unsigned char flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable;
147+  Ebml_WriteID(glob, SimpleBlock);
148   blockLength |= 0x10000000; // TODO check length < 0x0FFFFFFFF
149   Ebml_Serialize(glob, &blockLength, sizeof(blockLength), 4);
150   trackNumber |= 0x80;  // TODO check track nubmer < 128
151   Ebml_Write(glob, &trackNumber, 1);
152   // Ebml_WriteSigned16(glob, timeCode,2); //this is 3 bytes
153   Ebml_Serialize(glob, &timeCode, sizeof(timeCode), 2);
154-  unsigned char flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable;
155+  flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable;
156   Ebml_Write(glob, &flags, 1);
157   Ebml_Write(glob, data, dataLength);
158 }
159@@ -48,17 +53,18 @@ static UInt64 generateTrackID(unsigned int trackNumber) {
160   UInt64 r = rand();
161   r = r << 32;
162   r +=  rand();
163-  UInt64 rval = t ^ r;
164-  return rval;
165+//  UInt64 rval = t ^ r;
166+  return t ^ r;
167 }
168
169 void writeVideoTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing,
170                      char *codecId, unsigned int pixelWidth, unsigned int pixelHeight,
171                      double frameRate) {
172   EbmlLoc start;
173+  UInt64 trackID;
174   Ebml_StartSubElement(glob, &start, TrackEntry);
175   Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
176-  UInt64 trackID = generateTrackID(trackNumber);
177+  trackID = generateTrackID(trackNumber);
178   Ebml_SerializeUnsigned(glob, TrackUID, trackID);
179   Ebml_SerializeString(glob, CodecName, "VP8");  // TODO shouldn't be fixed
180
181@@ -78,9 +84,10 @@ void writeAudioTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing,
182                      char *codecId, double samplingFrequency, unsigned int channels,
183                      unsigned char *private, unsigned long privateSize) {
184   EbmlLoc start;
185+  UInt64 trackID;
186   Ebml_StartSubElement(glob, &start, TrackEntry);
187   Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
188-  UInt64 trackID = generateTrackID(trackNumber);
189+  trackID = generateTrackID(trackNumber);
190   Ebml_SerializeUnsigned(glob, TrackUID, trackID);
191   Ebml_SerializeUnsigned(glob, TrackType, 2); // audio is always 2
192   // I am using defaults for thesed required fields
193
194