diff --git a/examples/Makefile.am b/examples/Makefile.am
index 95b3516..2ea175e 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -15,7 +15,7 @@ get_bmp_LDADD = -lredwg
 dwg_ps_SOURCES = dwg_ps.c
 dwg_ps_LDADD = -lredwg -lps
 
-AM_CFLAGS = -Wextra
+AM_CFLAGS = -Wextra -I../src
 
 EXTRA_DIST = sample.dwg
 
diff --git a/examples/Makefile.in b/examples/Makefile.in
index 3e08039..e375083 100644
--- a/examples/Makefile.in
+++ b/examples/Makefile.in
@@ -198,7 +198,7 @@ get_bmp_SOURCES = get_bmp.c
 get_bmp_LDADD = -lredwg
 dwg_ps_SOURCES = dwg_ps.c
 dwg_ps_LDADD = -lredwg -lps
-AM_CFLAGS = -Wextra
+AM_CFLAGS = -Wextra -I ../src/
 EXTRA_DIST = sample.dwg
 all: all-am
 
diff --git a/examples/testSVG.c b/examples/testSVG.c
index a9fd27e..8881f10 100644
--- a/examples/testSVG.c
+++ b/examples/testSVG.c
@@ -21,9 +21,9 @@
 #include <string.h>
 #include <unistd.h>
 #include <math.h>
-
-#include "../src/bits.h"
 #include <dwg.h>
+#include "../src/bits.h"
+
 
 double model_xmin, model_ymin;
 double page_width, page_height, scale;
@@ -212,6 +212,7 @@ void output_BLOCK_HEADER(Dwg_Object_Ref* ref)
   printf("\t</g>\n");
 }
 
+
 void
 output_SVG(Dwg_Data* dwg)
 {
diff --git a/src/bits.c b/src/bits.c
index 48ac9cb..6fa51ee 100644
--- a/src/bits.c
+++ b/src/bits.c
@@ -623,7 +623,7 @@ bit_read_DD(Bit_Chain * dat, double default_value)
     return (bit_read_RD(dat));
   if (two_bit_code == 2)
     {
-      uchar_result = (char *) &default_value;
+      uchar_result = (unsigned char *) &default_value;
       uchar_result[4] = bit_read_RC(dat);
       uchar_result[5] = bit_read_RC(dat);
       uchar_result[0] = bit_read_RC(dat);
@@ -635,7 +635,7 @@ bit_read_DD(Bit_Chain * dat, double default_value)
     }
   else /* if (two_bit_code == 1) */
     {
-      uchar_result = (char *) &default_value;
+      uchar_result = (unsigned char *) &default_value;
       uchar_result[0] = bit_read_RC(dat);
       uchar_result[1] = bit_read_RC(dat);
       uchar_result[2] = bit_read_RC(dat);
@@ -659,9 +659,9 @@ bit_write_DD(Bit_Chain * dat, double value, double default_value)
     bit_write_BB(dat, 0);
   else
     {
-      uchar_value = (char *) &value;
-      uint_value = (int *) &value;
-      uint_default = (int *) &default_value;
+      uchar_value = (unsigned char *) &value;
+      uint_value = (unsigned int *) &value;
+      uint_default = (unsigned int *) &default_value;
       if (uint_value[0] == uint_default[0])
         {
           if (uint_value[1] != uint_default[1])
@@ -740,7 +740,7 @@ bit_read_H(Bit_Chain * dat, Dwg_Handle * handle)
       return (-1);
     }
 
-  val = (char *) &handle->value;
+  val = (unsigned char *) &handle->value;
   for (i = handle->size - 1; i >= 0; i--)
     val[i] = bit_read_RC(dat);
 
@@ -762,7 +762,7 @@ bit_write_H(Bit_Chain * dat, Dwg_Handle * handle)
       return;
     }
 
-  val = (char *) &handle->value;
+  val = (unsigned char *) &handle->value;
   for (i = 3; i >= 0; i--)
     if (val[i])
       break;
@@ -853,7 +853,7 @@ bit_read_TV(Bit_Chain * dat)
   unsigned char *chain;
 
   length = bit_read_BS(dat);
-  chain = (char *) malloc(length + 1);
+  chain = (unsigned char *) malloc(length + 1);
   for (i = 0; i < length; i++)
     {
       chain[i] = bit_read_RC(dat);
@@ -875,7 +875,7 @@ bit_write_TV(Bit_Chain * dat, unsigned char *chain)
   int i;
   int length;
 
-  length = strlen(chain);
+  length = strlen((const char *)chain);
   bit_write_BS(dat, length);
   for (i = 0; i < length; i++)
     bit_write_RC(dat, chain[i]);
@@ -903,7 +903,7 @@ bit_write_L(Bit_Chain * dat, long unsigned int value)
 {
   unsigned char *btk;
 
-  btk = (char *) value;
+  btk = (unsigned char *) value;
   bit_write_RC(dat, btk[3]);
   bit_write_RC(dat, btk[2]);
   bit_write_RC(dat, btk[1]);
@@ -922,9 +922,9 @@ bit_read_CMC(Bit_Chain * dat, Dwg_Color* color)
       color->rgb = bit_read_BL(dat);
       color->byte = bit_read_RC(dat);
       if (color->byte & 1)
-        color->name = bit_read_TV(dat);
+        color->name = (char*)bit_read_TV(dat);
       if (color->byte & 2)
-        color->book_name = bit_read_TV(dat);
+        color->book_name = (char*)bit_read_TV(dat);
     }
 }
 
@@ -939,9 +939,9 @@ bit_write_CMC(Bit_Chain * dat, Dwg_Color* color)
       bit_write_BL(dat, color->rgb);
       bit_write_RC(dat, color->byte);
       if (color->byte & 1)
-        bit_write_TV(dat, color->name);
+        bit_write_TV(dat,(unsigned char*) color->name);
       if (color->byte & 2)
-        bit_write_TV(dat, color->book_name);
+        bit_write_TV(dat, (unsigned char*)color->book_name);
     }
 }
 
@@ -987,14 +987,14 @@ bit_chain_alloc(Bit_Chain * dat)
 {
   if (dat->size == 0)
     {
-      dat->chain = calloc(1, CHAIN_BLOCK);
+      dat->chain = (unsigned char *)calloc(1, CHAIN_BLOCK);
       dat->size = CHAIN_BLOCK;
       dat->byte = 0;
       dat->bit = 0;
     }
   else
     {
-      dat->chain = realloc(dat->chain, dat->size + CHAIN_BLOCK);
+      dat->chain = (unsigned char *)realloc(dat->chain, dat->size + CHAIN_BLOCK);
       dat->size += CHAIN_BLOCK;
     }
 }
diff --git a/src/bits.h b/src/bits.h
index 60a6ada..30e4937 100644
--- a/src/bits.h
+++ b/src/bits.h
@@ -68,6 +68,9 @@ bit_write_BB(Bit_Chain * bit_datenaro, unsigned char value);
 BITCODE_4BITS
 bit_read_4BITS(Bit_Chain * dat);
 
+void
+bit_write_4BITS(char , Bit_Chain * bit_data);
+
 BITCODE_RC
 bit_read_RC(Bit_Chain * bit_datenaro);
 
diff --git a/src/common.h b/src/common.h
index edf7dbc..c271865 100644
--- a/src/common.h
+++ b/src/common.h
@@ -35,7 +35,7 @@ typedef enum DWG_VERSION_TYPE
 {
   R_BEFORE, R_13, R_14, R_2000, R_2004, R_2007, R_AFTER
 } Dwg_Version_Type;
-char version_codes[7][7];
+extern char version_codes[7][7];
 
 /**
  Data types (including compressed forms) used trough the file
diff --git a/src/decode.c b/src/decode.c
index fcbf09c..221c8f1 100644
--- a/src/decode.c
+++ b/src/decode.c
@@ -28,7 +28,11 @@
 #include "bits.h"
 #include "dwg.h"
 #include "decode.h"
+#include <iostream>
 
+using  namespace std;
+int decode_R2004(Bit_Chain* dat, Dwg_Data * dwg);
+int decode_R2007(Bit_Chain* dat, Dwg_Data * dwg);
 
 /*--------------------------------------------------------------------------------
  * Welcome to the dark side of the moon...
@@ -139,7 +143,7 @@
 #define HANDLE_VECTOR(name, sizefield, code) HANDLE_VECTOR_N(name, FIELD_VALUE(sizefield), code)
 
 #define REACTORS(code)\
-  FIELD_VALUE(reactors) = malloc(sizeof(BITCODE_H) * obj->tio.object->num_reactors);\
+  FIELD_VALUE(reactors) = (BITCODE_H*) malloc(sizeof(BITCODE_H) * obj->tio.object->num_reactors);\
   for (vcount=0; vcount<obj->tio.object->num_reactors; vcount++)\
     {\
       FIELD_HANDLE(reactors[vcount], code);\
@@ -179,7 +183,7 @@
 
 #define DWG_ENTITY(token) \
 static void \
-dwg_decode_##token(Bit_Chain * dat, Dwg_Object * obj)\
+ dwg_decode_##token (Bit_Chain * dat, Dwg_Object * obj)\
 {\
   int vcount, rcount, rcount2, rcount3;\
   if (loglevel)\
@@ -188,8 +192,8 @@ dwg_decode_##token(Bit_Chain * dat, Dwg_Object * obj)\
 	Dwg_Data* dwg = obj->parent;\
 	dwg->num_entities++;\
 	obj->supertype = DWG_SUPERTYPE_ENTITY;\
-	obj->tio.entity = malloc (sizeof (Dwg_Object_Entity));\
-	obj->tio.entity->tio.token = calloc (sizeof (Dwg_Entity_##token), 1);\
+	obj->tio.entity = (Dwg_Object_Entity*)malloc (sizeof (Dwg_Object_Entity));	\
+	obj->tio.entity->tio.token = (Dwg_Entity_##token *)calloc (sizeof (Dwg_Entity_##token), 1); \
 	ent = obj->tio.entity->tio.token;\
   _obj=ent;\
   obj->tio.entity->object = obj;\
@@ -201,18 +205,15 @@ dwg_decode_##token(Bit_Chain * dat, Dwg_Object * obj)\
 
 #define DWG_ENTITY_END }
 
-#define DWG_OBJECT(token) \
-static void \
-dwg_decode_##token(Bit_Chain * dat, Dwg_Object * obj)\
-{\
+#define DWG_OBJECT(token) static void  dwg_decode_ ## token (Bit_Chain * dat, Dwg_Object * obj) {\
   int vcount, rcount, rcount2, rcount3;\
   if (loglevel)\
     fprintf (stderr, "Object " #token ":\n");\
 	Dwg_Object_##token *_obj;\
 	Dwg_Data* dwg = obj->parent;\
 	obj->supertype = DWG_SUPERTYPE_OBJECT;\
-	obj->tio.object = malloc (sizeof (Dwg_Object_Object));\
-	obj->tio.object->tio.token = calloc (sizeof (Dwg_Object_##token), 1);\
+	obj->tio.object = (Dwg_Object_Object*)malloc (sizeof (Dwg_Object_Object));	\
+	obj->tio.object->tio.token = (Dwg_Object_##token * ) calloc (sizeof (Dwg_Object_##token), 1); \
   obj->tio.object->object = obj;\
 	if (dwg_decode_object (dat, obj->tio.object)) return;\
 	_obj = obj->tio.object->tio.token;\
@@ -243,6 +244,8 @@ long unsigned int ktl_lastaddress;
 
 static int loglevel = 1;
 
+int decode_R13_R15(Bit_Chain* dat, Dwg_Data * dwg); // froward
+
 /*--------------------------------------------------------------------------------
  * Public function definitions
  */
@@ -259,7 +262,7 @@ dwg_decode_data(Bit_Chain * dat, Dwg_Data * dwg)
   /* Version */
   dat->byte = 0;
   dat->bit = 0;
-  strncpy(version, dat->chain, 6);
+  strncpy(version, (const char *)dat->chain, 6);
   version[6] = '\0';
 
   dwg->header.version = 0;
@@ -279,7 +282,7 @@ dwg_decode_data(Bit_Chain * dat, Dwg_Data * dwg)
         "This file's version code is: %s\n", version);
       return -1;
     }
-  dat->version = dwg->header.version;
+  dat->version = (Dwg_Version_Type)dwg->header.version;
   if (loglevel) fprintf(stderr,
       "This file's version code is: %s\n", version);
 
@@ -438,7 +441,7 @@ decode_R13_R15(Bit_Chain* dat, Dwg_Data * dwg)
       dat->byte = dwg->header.section[5].address;
       dwg->unknown1.size = DWG_UNKNOWN1_SIZE;
       dwg->unknown1.byte = dwg->unknown1.bit = 0;
-      dwg->unknown1.chain = malloc(dwg->unknown1.size);
+      dwg->unknown1.chain = (unsigned char*)malloc(dwg->unknown1.size);
       memcpy(dwg->unknown1.chain, &dat->chain[dat->byte], dwg->unknown1.size);
       //bit_explore_chain ((Bit_Chain *) &dwg->unknown1, dwg->unknown1.size);
       //bit_print ((Bit_Chain *) &dwg->unknown1, dwg->unknown1.size);
@@ -463,7 +466,7 @@ decode_R13_R15(Bit_Chain* dat, Dwg_Data * dwg)
             fprintf(stderr, "        PICTURE (end): %8X\n",
                 (unsigned int) dat->byte);
           dwg->picture.size = (dat->byte - 16) - start_address;
-          dwg->picture.chain = (char *) malloc(dwg->picture.size);
+          dwg->picture.chain = (unsigned char *) malloc(dwg->picture.size);
           memcpy(dwg->picture.chain, &dat->chain[start_address],
               dwg->picture.size);
         }
@@ -536,23 +539,22 @@ decode_R13_R15(Bit_Chain* dat, Dwg_Data * dwg)
     {
       unsigned int idc;
 
-      idc = dwg->num_classes;
-      if (idc == 0)
-        dwg->class = (Dwg_Class *) malloc(sizeof(Dwg_Class));
+      idc = dwg->num_classes; if (idc == 0)
+        dwg->dwg_class = (Dwg_Class *) malloc(sizeof(Dwg_Class));
       else
-        dwg->class = (Dwg_Class *) realloc(dwg->class, (idc + 1)
+        dwg->dwg_class = (Dwg_Class *) realloc(dwg->dwg_class, (idc + 1)
             * sizeof(Dwg_Class));
 
-      dwg->class[idc].number = bit_read_BS(dat);
-      dwg->class[idc].version = bit_read_BS(dat);
-      dwg->class[idc].appname = bit_read_TV(dat);
-      dwg->class[idc].cppname = bit_read_TV(dat);
-      dwg->class[idc].dxfname = bit_read_TV(dat);
-      dwg->class[idc].wasazombie = bit_read_B(dat);
-      dwg->class[idc].item_class_id = bit_read_BS(dat);
+      dwg->dwg_class[idc].number = bit_read_BS(dat);
+      dwg->dwg_class[idc].version = bit_read_BS(dat);
+      dwg->dwg_class[idc].appname = bit_read_TV(dat);
+      dwg->dwg_class[idc].cppname = bit_read_TV(dat);
+      dwg->dwg_class[idc].dxfname = bit_read_TV(dat);
+      dwg->dwg_class[idc].wasazombie = bit_read_B(dat);
+      dwg->dwg_class[idc].item_class_id = bit_read_BS(dat);
 
-      if (strcmp(dwg->class[idc].dxfname, "LAYOUT") == 0)
-        dwg->dwg_ot_layout = dwg->class[idc].number;
+      if (strcmp((const char *)dwg->dwg_class[idc].dxfname, "LAYOUT") == 0)
+        dwg->dwg_ot_layout = dwg->dwg_class[idc].number;
 
       dwg->num_classes++;
       if (dwg->num_classes > 100)
@@ -953,15 +955,22 @@ decompress_R2004_section(Bit_Chain* dat, char *decomp,
         }
       else if (opcode1 >= 0x12 && opcode1 <= 0x1F)
         {
-          assert(0);  // Not seen yet - could not check
+	  std::cerr << "got opcode1:" <<opcode1 << std::endl;
+	  //          assert(0);  // Not seen yet - could not check
 
           comp_bytes  = (opcode1 & 0x0F) + 2;
           comp_offset = read_two_byte_offset(dat, &lit_length) - 0x3FFF;
 
+	  std::cerr << "got offset:" <<comp_offset << std::endl;
+
           if (lit_length != 0)
             opcode1 = 0x00;
           else
-            lit_length = read_literal_length(dat, &opcode1);
+	    {
+	      lit_length = read_literal_length(dat, &opcode1);
+	      std::cerr << "got read opecode:" <<opcode1 << std::endl;
+	    }
+
         }
       else if (opcode1 == 0x10)
         {
@@ -1015,7 +1024,7 @@ read_R2004_section_map(Bit_Chain* dat, Dwg_Data * dwg,
   dwg->header.section = 0;
 
   // allocate memory to hold decompressed data
-  decomp = malloc(decomp_data_size * sizeof(char));
+  decomp = (char *)malloc(decomp_data_size * sizeof(char));
   if (decomp == 0)
     return;   // No memory
 
@@ -1103,7 +1112,7 @@ read_R2004_section_info(Bit_Chain* dat, Dwg_Data *dwg,
   int unknown;
   Dwg_Section *section;
 
-  decomp = malloc(decomp_data_size * sizeof(char));
+  decomp = (char *)malloc(decomp_data_size * sizeof(char));
   if (decomp == 0)
     return;   // No memory
 
@@ -1266,7 +1275,7 @@ read_2004_class_section(Bit_Chain* dat, Dwg_Data *dwg)
         (unsigned int) es.fields.checksum_2);
     }
   
-  decomp = malloc(info->max_decomp_size * sizeof(char));
+  decomp = (char *)malloc(info->max_decomp_size * sizeof(char));
   if (decomp == 0)
     return;   // No memory
 
@@ -1274,7 +1283,7 @@ read_2004_class_section(Bit_Chain* dat, Dwg_Data *dwg)
 
   class_dat.bit     = 0;
   class_dat.byte    = 0;
-  class_dat.chain   = decomp;
+  class_dat.chain   = (unsigned char *)decomp;
   class_dat.size    = info->max_decomp_size;
   class_dat.version = dat->version;
 
@@ -1297,18 +1306,18 @@ read_2004_class_section(Bit_Chain* dat, Dwg_Data *dwg)
 
           idc = dwg->num_classes;
           if (idc == 0)
-            dwg->class = (Dwg_Class *) malloc(sizeof(Dwg_Class));
+            dwg->dwg_class = (Dwg_Class *) malloc(sizeof(Dwg_Class));
           else
-            dwg->class = (Dwg_Class *) realloc(dwg->class, (idc + 1)
+            dwg->dwg_class = (Dwg_Class *) realloc(dwg->dwg_class, (idc + 1)
                 * sizeof(Dwg_Class));
 
-          dwg->class[idc].number        = bit_read_BS(&class_dat);
-          dwg->class[idc].version       = bit_read_BS(&class_dat);
-          dwg->class[idc].appname       = bit_read_TV(&class_dat);
-          dwg->class[idc].cppname       = bit_read_TV(&class_dat);
-          dwg->class[idc].dxfname       = bit_read_TV(&class_dat);
-          dwg->class[idc].wasazombie    = bit_read_B(&class_dat);
-          dwg->class[idc].item_class_id = bit_read_BS(&class_dat);
+          dwg->dwg_class[idc].number        = bit_read_BS(&class_dat);
+          dwg->dwg_class[idc].version       = bit_read_BS(&class_dat);
+          dwg->dwg_class[idc].appname       = bit_read_TV(&class_dat);
+          dwg->dwg_class[idc].cppname       = bit_read_TV(&class_dat);
+          dwg->dwg_class[idc].dxfname       = bit_read_TV(&class_dat);
+          dwg->dwg_class[idc].wasazombie    = bit_read_B(&class_dat);
+          dwg->dwg_class[idc].item_class_id = bit_read_BS(&class_dat);
 
           num_objects   = bit_read_BL(&class_dat);  // DXF 91
           dwg_version   = bit_read_BS(&class_dat);  // Dwg Version
@@ -1319,15 +1328,15 @@ read_2004_class_section(Bit_Chain* dat, Dwg_Data *dwg)
           if (loglevel)
             {
               fprintf(stderr, "-------------------\n");
-              fprintf(stderr, "Number:           %d\n", dwg->class[idc].number);
-              fprintf(stderr, "Version:          %x\n", dwg->class[idc].version);
-              fprintf(stderr, "Application name: %s\n", dwg->class[idc].appname);
-              fprintf(stderr, "C++ class name:   %s\n", dwg->class[idc].cppname);
-              fprintf(stderr, "DXF record name:  %s\n", dwg->class[idc].dxfname);
+              fprintf(stderr, "Number:           %d\n", dwg->dwg_class[idc].number);
+              fprintf(stderr, "Version:          %x\n", dwg->dwg_class[idc].version);
+              fprintf(stderr, "Application name: %s\n", dwg->dwg_class[idc].appname);
+              fprintf(stderr, "C++ class name:   %s\n", dwg->dwg_class[idc].cppname);
+              fprintf(stderr, "DXF record name:  %s\n", dwg->dwg_class[idc].dxfname);
             }
 
-          if (strcmp(dwg->class[idc].dxfname, "LAYOUT") == 0)
-            dwg->dwg_ot_layout = dwg->class[idc].number;
+          if (strcmp((const char *)dwg->dwg_class[idc].dxfname, "LAYOUT") == 0)
+            dwg->dwg_ot_layout = dwg->dwg_class[idc].number;
 
           dwg->num_classes++;
 
@@ -1813,7 +1822,7 @@ dwg_decode_entity(Bit_Chain * dat, Dwg_Object_Entity * ent)
       ent->extended_size = 0;
       ent->picture_exists = 0;
       ent->num_handles = 0;
-      return;
+      return 0;
     }
 
   ent->extended_size = 0;
@@ -1837,13 +1846,13 @@ dwg_decode_entity(Bit_Chain * dat, Dwg_Object_Entity * ent)
         }
       if (ent->extended_size == 0)
         {
-          ent->extended = malloc(size);
+          ent->extended = (char *)malloc(size);
           ent->extended_size = size;
         }
       else
         {
           ent->extended_size += size;
-          ent->extended = realloc(ent->extended, ent->extended_size);
+          ent->extended = (char *)realloc(ent->extended, ent->extended_size);
         }
       error = bit_read_H(dat, &ent->extended_handle);
       if (error)
@@ -1857,7 +1866,7 @@ dwg_decode_entity(Bit_Chain * dat, Dwg_Object_Entity * ent)
       ent->picture_size = bit_read_RL(dat);
       if (ent->picture_size < 210210)
         {
-          ent->picture = malloc(ent->picture_size);
+          ent->picture = (char *)malloc(ent->picture_size);
           for (i = 0; i < ent->picture_size; i++)
             ent->picture[i] = bit_read_RC(dat);
         }
@@ -1950,17 +1959,17 @@ dwg_decode_object(Bit_Chain * dat, Dwg_Object_Object * ord)
           ord->bitsize = 0;
           ord->extended_size = 0;
           ord->num_handles = 0;
-          return;
+          return 0;
         }
       if (ord->extended_size == 0)
         {
-          ord->extended = malloc(size);
+          ord->extended = (unsigned char *)malloc(size);
           ord->extended_size = size;
         }
       else
         {
           ord->extended_size += size;
-          ord->extended = realloc(ord->extended, ord->extended_size);
+          ord->extended = (unsigned char *)realloc(ord->extended, ord->extended_size);
         }
       error = bit_read_H(dat, &ord->extended_handle);
       if (error)
@@ -2638,7 +2647,7 @@ dwg_decode_common_entity_handle_data(Bit_Chain * dat, Dwg_Object * obj)
   //	ent->subentity_ref_handle = dwg_decode_handleref_with_code (dat, obj, obj->parent, 3);
 
   if (ent->num_reactors)
-    ent->reactors = malloc(ent->num_reactors * sizeof(Dwg_Object_Ref*));
+    ent->reactors = (Dwg_Object_Ref**) malloc(ent->num_reactors * sizeof(Dwg_Object_Ref*));
   for (i = 0; i < ent->num_reactors; i++)
     {
       ent->reactors[i] = dwg_decode_handleref_with_code(dat, obj, obj->parent, 4);
@@ -2693,97 +2702,97 @@ dwg_decode_variable_type(Dwg_Data * dwg, Bit_Chain * dat, Dwg_Object* obj)
     return 1;
   int i = obj->type - 500;
 
-  if (!strcmp(dwg->class[i].dxfname, "DICTIONARYVAR"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "DICTIONARYVAR"))
     {
       dwg_decode_DICTIONARYVAR(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "DICTIONARYWDFLT"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "DICTIONARYWDFLT"))
     {
       //dwg_decode_DICTIONARYWDLFT(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "HATCH"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "HATCH"))
     {
       dwg_decode_HATCH(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "IDBUFFER"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IDBUFFER"))
     {
       dwg_decode_IDBUFFER(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "IMAGE"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGE"))
     {
       dwg_decode_IMAGE(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "IMAGEDEF"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGEDEF"))
     {
       dwg_decode_IMAGEDEF(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "IMAGEDEFREACTOR"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGEDEFREACTOR"))
     {
       dwg_decode_IMAGEDEFREACTOR(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "LAYER_INDEX"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LAYER_INDEX"))
     {
       dwg_decode_LAYER_INDEX(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "LAYOUT"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LAYOUT"))
     {
       dwg_decode_LAYOUT(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "LWPLINE"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LWPLINE"))
     {
       dwg_decode_LWPLINE(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "OLE2FRAME"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "OLE2FRAME"))
     {
       dwg_decode_OLE2FRAME(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "PLACEHOLDER"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "PLACEHOLDER"))
     {
 //TODO:      dwg_decode_PLACEHOLDER(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "RASTERVARIABLES"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "RASTERVARIABLES"))
     {
       dwg_decode_RASTERVARIABLES(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "SORTENTSTABLE"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SORTENTSTABLE"))
     {
       dwg_decode_SORTENTSTABLE(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "SPATIAL_FILTER"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SPATIAL_FILTER"))
     {
       dwg_decode_SPATIAL_FILTER(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "SPATIAL_INDEX"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SPATIAL_INDEX"))
     {
       dwg_decode_SPATIAL_INDEX(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "VBA_PROJECT"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "VBA_PROJECT"))
     {
 //TODO:      dwg_decode_VBA_PROJECT(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "WIPEOUTVARIABLE"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "WIPEOUTVARIABLE"))
     {
 //TODO:      dwg_decode_WIPEOUTVARIABLE(dat, obj);
       return 0;
     }
-  if (!strcmp(dwg->class[i].dxfname, "XRECORD"))
+  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "XRECORD"))
     {
       dwg_decode_XRECORD(dat, obj);
       return 0;
@@ -3059,7 +3068,7 @@ dwg_decode_add_object(Dwg_Data * dwg, Bit_Chain * dat,
         if (!dwg_decode_variable_type(dwg, dat, obj))
           {
             obj->supertype = DWG_SUPERTYPE_UNKNOWN;
-            obj->tio.unknown = malloc(obj->size);
+            obj->tio.unknown = (unsigned char*)malloc(obj->size);
             memcpy(obj->tio.unknown, &dat->chain[object_address], obj->size);
           }
       }
diff --git a/src/dwg.c b/src/dwg.c
index e5ca5e2..539e561 100644
--- a/src/dwg.c
+++ b/src/dwg.c
@@ -64,7 +64,7 @@ dwg_read_file(char *filename, Dwg_Data * dwg_data)
   bit_chain.bit = 0;
   bit_chain.byte = 0;
   bit_chain.size = attrib.st_size;
-  bit_chain.chain = (char *) malloc(bit_chain.size);
+  bit_chain.chain = (unsigned char *) malloc(bit_chain.size);
   if (!bit_chain.chain)
     {
       fprintf(stderr, "Not enough memory.\n");
@@ -102,7 +102,7 @@ dwg_write_file(char *filename, Dwg_Data * dwg_data)
   FILE *dt;
   struct stat atrib;
   Bit_Chain bit_chain;
-  bit_chain.version = dwg_data->header.version;
+  bit_chain.version = (Dwg_Version_Type)dwg_data->header.version;
 
   /* Encode the DWG struct
    bit_chain.size = 0;
diff --git a/src/dwg.h b/src/dwg.h
index 96b7e10..24ddd8a 100644
--- a/src/dwg.h
+++ b/src/dwg.h
@@ -23,8 +23,8 @@
 #define DWG_H
 
 #ifdef __cplusplus
-extern "C"
-  {
+//extern "C"
+//  {
 #endif
 
 #define BITCODE_DOUBLE double
@@ -2724,7 +2724,7 @@ typedef struct _dwg_struct
 
   Dwg_Header_Variables header_vars;
   unsigned int num_classes;
-  Dwg_Class *class;
+  Dwg_Class * dwg_class;
 
   long unsigned int num_objects;
   Dwg_Object *object;
@@ -2804,8 +2804,14 @@ dwg_get_entity_layer(Dwg_Object_Entity *);
 Dwg_Object*
 dwg_next_object(Dwg_Object* obj);
 
+
+double dwg_page_x_min(Dwg_Data *dwg);
+double dwg_page_x_max(Dwg_Data *dwg);
+double dwg_page_y_min(Dwg_Data *dwg);
+double dwg_page_y_max(Dwg_Data *dwg);
+
 #ifdef __cplusplus
-}
+//}
 #endif
 
 #endif
diff --git a/src/encode.c b/src/encode.c
index 9c3e19d..92d6b49 100644
--- a/src/encode.c
+++ b/src/encode.c
@@ -154,8 +154,7 @@ bit_write_BE(dat, FIELD_VALUE(name.x), FIELD_VALUE(name.y), FIELD_VALUE(name.z))
 //TODO unify REPEAT macros!
 
 #define DWG_ENTITY(token) \
-static void \
-dwg_encode_##token(Dwg_Object* obj, Dwg_Entity_##token * _obj, Bit_Chain * dat)\
+static void dwg_encode_##token (Dwg_Object* obj, Dwg_Entity_##token * _obj, Bit_Chain * dat)\
 {\
   int vcount, rcount, rcount2, rcount3;\
 	Dwg_Data* dwg = obj->parent;\
@@ -164,9 +163,7 @@ dwg_encode_##token(Dwg_Object* obj, Dwg_Entity_##token * _obj, Bit_Chain * dat)\
 
 #define DWG_ENTITY_END }
 
-#define DWG_OBJECT(token) \
-static void\
-dwg_encode_##token(Dwg_Object* obj, Dwg_Object_##token *_obj, Bit_Chain * dat)\
+#define DWG_OBJECT(token) static void dwg_encode_##token (Dwg_Object* obj, Dwg_Object_##token *_obj, Bit_Chain * dat)\
 {\
   int vcount, rcount, rcount2, rcount3;\
 	Dwg_Data* dwg = obj->parent;
@@ -222,7 +219,7 @@ dwg_encode_chains(Dwg_Data * dwg, Bit_Chain * dat)
    * Header variables
    */
   //strcpy (dat->chain, dwg->header.version); // Chain version: should be AC1015
-  strcpy(dat->chain, "AC1015"); // Chain version: should be AC1015
+  strcpy((char *)dat->chain, "AC1015"); // Chain version: should be AC1015
   dat->byte += 6;
 
   for (i = 0; i < 5; i++)
@@ -329,13 +326,13 @@ dwg_encode_chains(Dwg_Data * dwg, Bit_Chain * dat)
 
   for (i = 0; i < dwg->num_classes; i++)
     {
-      bit_write_BS(dat, dwg->class[i].number);
-      bit_write_BS(dat, dwg->class[i].version);
-      bit_write_TV(dat, dwg->class[i].appname);
-      bit_write_TV(dat, dwg->class[i].cppname);
-      bit_write_TV(dat, dwg->class[i].dxfname);
-      bit_write_B(dat, dwg->class[i].wasazombie);
-      bit_write_BS(dat, dwg->class[i].item_class_id);
+      bit_write_BS(dat, dwg->dwg_class[i].number);
+      bit_write_BS(dat, dwg->dwg_class[i].version);
+      bit_write_TV(dat, dwg->dwg_class[i].appname);
+      bit_write_TV(dat, dwg->dwg_class[i].cppname);
+      bit_write_TV(dat, dwg->dwg_class[i].dxfname);
+      bit_write_B(dat, dwg->dwg_class[i].wasazombie);
+      bit_write_BS(dat, dwg->dwg_class[i].item_class_id);
     }
 
   /* Write the size of the section at its beginning
