diff --git a/C/pl-yap.c b/C/pl-yap.c
index 32c394f6a..171c60cf4 100755
--- a/C/pl-yap.c
+++ b/C/pl-yap.c
@@ -18,6 +18,11 @@
 #if HAVE_MATH_H
 #include <math.h>
 #endif
+#if __WINDOWS__
+#include <process.h>
+
+#define getpid _getpid
+#endif
 
 #ifdef HAVE_LIMITS_H
 #include <limits.h>
diff --git a/H/pl-incl.h b/H/pl-incl.h
index c5f1927dc..8c3a0eb2d 100755
--- a/H/pl-incl.h
+++ b/H/pl-incl.h
@@ -4,16 +4,14 @@
 #define PL_INCL_H 1
 
 #ifndef __WINDOWS__
-#if defined(_MSC_VER) || defined(__MINGW32__)
+#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MSYS__)
 #define __WINDOWS__ 1
 #endif
 #endif
 
 #ifdef __WINDOWS__
-#if HAVE_WINSOCK2_H
-#include <winsock2.h>
-#endif
 
+#include <winsock2.h>
 #include <windows.h>
 
 #if HAVE_XOS_H
diff --git a/configure.in b/configure.in
index bce9a1e83..4e3d0d424 100755
--- a/configure.in
+++ b/configure.in
@@ -517,6 +517,7 @@ AC_PROG_INSTALL
 AC_PROG_RANLIB
 AC_CHECK_TOOL(AR,[ar],:)
 AC_PATH_PROG(INSTALL_INFO,install-info,true,$PATH:/sbin:/usr/sbin:/usr/etc:/usr/local/sbin)
+AC_CHECK_HEADERS(winsock.h winsock2.h)
 AC_PATH_PROG(SHELL,sh)
 AC_CHECK_TOOL([INDENT], [indent], [:])
 
@@ -1248,7 +1249,7 @@ AC_CHECK_HEADERS(sys/shm.h sys/socket.h sys/stat.h)
 AC_CHECK_HEADERS(sys/time.h sys/times.h sys/types.h)
 AC_CHECK_HEADERS(sys/ucontext.h sys/un.h sys/wait.h)
 AC_CHECK_HEADERS(time.h ucontext.h unistd.h utime.h)
-AC_CHECK_HEADERS(wctype.h winsock.h winsock2.h)
+AC_CHECK_HEADERS(wctype.h)
 AC_CHECK_HEADERS(zlib.h zutil.h)
 AC_CHECK_HEADERS(mach-o/dyld.h LibLoaderAPI.h)
 
@@ -1319,6 +1320,24 @@ AC_CHECK_SIZEOF(void *,4)
 
 AC_DEFINE_UNQUOTED(CELLSIZE, $ac_cv_sizeof_void_p, [size of a cell in bytes.])
 
+dnl check whether int64_t exists
+AC_MSG_CHECKING(whether int64_t is defined)
+AC_CACHE_VAL(yap_cv_int64_t_defined,[
+AC_TRY_RUN(
+	#include <stdint.h>
+	int main() {
+	   int64_t i;
+	   return 0;
+	}
+	,
+	yap_cv_int64_t_defined=yes,yap_cv_int64_t_defined=no,yap_cv_int64_t_defined=no)])
+AC_MSG_RESULT($yap_cv_int64_t_defined)
+if test $yap_cv_int64_t_defined = yes
+then
+  AC_DEFINE_UNQUOTED(INT64_T_DEFINED,1,[type of int64_t])
+fi
+AC_MSG_CHECKING(for int64_t)
+AC_SUBST(INT64_T_DEFINED)
 
 dnl check type of malloc (i.e. char *  or void *)
 AC_MSG_CHECKING(for type of malloc)
diff --git a/console/LGPL/pl-ntconsole.c b/console/LGPL/pl-ntconsole.c
index 67a54a9f7..ebc65703b 100755
--- a/console/LGPL/pl-ntconsole.c
+++ b/console/LGPL/pl-ntconsole.c
@@ -73,15 +73,19 @@ static IOFUNCTIONS *saved_functions;
 
 static void
 Message(const char *fm, ...)
-{ char buf[1024];
+{ LPWSTR wbuf;
+  char buf[1024];
+
   va_list(args);
-
-  return;
-
   va_start(args, fm);
-  vsprintf(buf, fm, args);
-  MessageBox(NULL, buf, "YAP-Prolog", MB_OK|MB_TASKMODAL);
+  vsprintf(buf, fm, args); 
   va_end(args);
+  wbuf = (LPWSTR)malloc(1024*sizeof(LPWSTR *));
+  if (!wbuf)
+    return;
+  MultiByteToWideChar( 0,0, (LPCCH)buf, -1, wbuf, 0);
+  MessageBox(NULL, wbuf, L"YAP-Prolog", MB_OK|MB_TASKMODAL);
+  free((void *)wbuf);
 }
 
 
diff --git a/include/SWI-Prolog.h b/include/SWI-Prolog.h
index afb632bb7..a6fcf21dc 100755
--- a/include/SWI-Prolog.h
+++ b/include/SWI-Prolog.h
@@ -888,8 +888,6 @@ PL_EXPORT(void)         PL_YAP_InitSWIIO(struct SWI_IO *swio);
 
 #endif /* _FLI_H_INCLUDED */
 
-#ifdef _WIN32
-#if O_PLMT
+#ifdef __WINDOWS__
 X_API int PL_w32thread_raise(DWORD id, int sig);
 #endif
-#endif
diff --git a/include/YapInterface.h b/include/YapInterface.h
index b29d632d5..19b56be16 100755
--- a/include/YapInterface.h
+++ b/include/YapInterface.h
@@ -1599,6 +1599,20 @@ __BEGIN_DECLS
 #define X_API
 #endif
 
+#ifndef Int_FORMAT
+
+#if _WIN64
+#define Int_FORMAT "%I64d"
+#define Int_ANYFORMAT "%I64i"
+#define UInt_FORMAT "%I64u"
+#else
+#define Int_FORMAT "%ld"
+#define Int_ANYFORMAT "%li"
+#define UInt_FORMAT "%lu"
+#endif
+
+#endif /* portable form of formatted output for Prolog terms */
+
 /* Primitive Functions */
 
 #define YAP_Deref(t)  (t)
diff --git a/library/rltree/yap_rl.c b/library/rltree/yap_rl.c
index 344d7ed95..2bfab6477 100644
--- a/library/rltree/yap_rl.c
+++ b/library/rltree/yap_rl.c
@@ -27,9 +27,9 @@ Last rev: $Id: yap_rl.c,v 1.1 2008-03-26 23:05:22 nunofonseca Exp $
 #include "range_list.h"
 #include <YapInterface.h>
 
-#define  IDTYPE long
-#define  PTR2ID(ptr) (IDTYPE)ptr
-#define  ID2PTR(id)  (RL_Tree*)id
+#define  IDTYPE YAP_Int
+#define  PTR2ID(ptr) ((IDTYPE)(ptr))
+#define  ID2PTR(id)  ((RL_Tree*)(id))
 
 
 /* ############################################################ */
diff --git a/library/tries/base_itries.c b/library/tries/base_itries.c
index 7b7554c64..0d912020a 100644
--- a/library/tries/base_itries.c
+++ b/library/tries/base_itries.c
@@ -45,7 +45,7 @@ void itrie_data_save(TrNode node, FILE *file) {
   TrData data;
 
   data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
-  fprintf(file, "%ld %ld %ld ", TrData_pos(data), TrData_neg(data), TrData_timestamp(data));
+  fprintf(file, Int_FORMAT " " Int_FORMAT " " Int_FORMAT " ", TrData_pos(data), TrData_neg(data), TrData_timestamp(data));
   return;
 }
 
@@ -55,7 +55,7 @@ void itrie_data_load(TrNode node, YAP_Int depth, FILE *file) {
   TrData data;
   YAP_Int pos, neg, timestamp;
 
-  fscanf(file, "%ld %ld %ld", &pos, &neg, &timestamp);
+  fscanf(file,  Int_FORMAT " "  Int_FORMAT " "  Int_FORMAT, &pos, &neg, &timestamp);
   new_itrie_data(data, CURRENT_ITRIE, node, pos, neg, timestamp, depth);
   PUT_DATA_IN_LEAF_TRIE_NODE(node, data);
   return;
@@ -67,7 +67,7 @@ void itrie_data_print(TrNode node) {
   TrData data;
 
   data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node);
-  printf("   pos: %ld neg: %ld timestamp: %ld\n", TrData_pos(data), TrData_neg(data), TrData_timestamp(data));
+  printf("   pos: " Int_FORMAT " neg: " Int_FORMAT "  timestamp: " Int_FORMAT "\\n", TrData_pos(data), TrData_neg(data), TrData_timestamp(data));
   return;
 }
 
diff --git a/library/tries/base_itries.h b/library/tries/base_itries.h
index f8bab1723..11a08eef9 100644
--- a/library/tries/base_itries.h
+++ b/library/tries/base_itries.h
@@ -73,8 +73,8 @@ typedef struct itrie_data {
 #define SIZEOF_TR_DATA        sizeof(TYPE_TR_DATA)
 #define SIZEOF_TR_DATA_BUCKET sizeof(TYPE_TR_DATA *)
 
-#define AS_TR_ENTRY_NEXT(ADDR) (TrEntry)((unsigned long int)(ADDR) - sizeof(struct trie_node *) - sizeof(struct itrie_data **) - sizeof(struct itrie_data *))
-#define AS_TR_DATA_NEXT(ADDR)  (TrData)((unsigned long int)(ADDR) - sizeof(struct itrie_entry *) - sizeof(struct trie_node *))
+#define AS_TR_ENTRY_NEXT(ADDR) (TrEntry)((YAP_UInt)(ADDR) - sizeof(struct trie_node *) - sizeof(struct itrie_data **) - sizeof(struct itrie_data *))
+#define AS_TR_DATA_NEXT(ADDR)  (TrData)((YAP_UInt)(ADDR) - sizeof(struct itrie_entry *) - sizeof(struct trie_node *))
 
 
 
diff --git a/library/tries/base_tries.h b/library/tries/base_tries.h
index e21e3274a..01c070daa 100644
--- a/library/tries/base_tries.h
+++ b/library/tries/base_tries.h
@@ -52,8 +52,8 @@ typedef struct trie_data {
 #define SIZEOF_TR_ENTRY       sizeof(TYPE_TR_ENTRY)
 #define SIZEOF_TR_DATA        sizeof(TYPE_TR_DATA)
 
-#define AS_TR_ENTRY_NEXT(ADDR) (TrEntry)((unsigned long int)(ADDR) - sizeof(struct trie_node *) - 3 * sizeof(struct trie_data *))
-#define AS_TR_DATA_NEXT(ADDR)  (TrData)((unsigned long int)(ADDR) - sizeof(struct trie_entry *) - sizeof(struct trie_node *))
+#define AS_TR_ENTRY_NEXT(ADDR) (TrEntry)((YAP_UInt)(ADDR) - sizeof(struct trie_node *) - 3 * sizeof(struct trie_data *))
+#define AS_TR_DATA_NEXT(ADDR)  (TrData)((YAP_UInt)(ADDR) - sizeof(struct trie_entry *) - sizeof(struct trie_node *))
 
 
 
diff --git a/library/tries/core_dbtries.c b/library/tries/core_dbtries.c
index fad498621..e780a577b 100644
--- a/library/tries/core_dbtries.c
+++ b/library/tries/core_dbtries.c
@@ -264,7 +264,7 @@ void displaynode(TrNode node) {
       {printf("1\n");} else {printf("2\n");}
   printf("bye\n");*/
     if (IS_HASH_NODE(node))
-      printf("HASH n%i, b%i, p%li\n", TrHash_num_nodes((TrHash) node), TrHash_num_buckets((TrHash) node), (long) node);
+      printf("HASH n%i, b%i, p%p\n", TrHash_num_nodes((TrHash) node), TrHash_num_buckets((TrHash) node), node);
     else if (TrNode_entry(node) == PairInitTag)
       printf("PairInitTag\n");
     else if (TrNode_entry(node) == PairEndTag)
@@ -272,7 +272,7 @@ void displaynode(TrNode node) {
     else if (IS_FUNCTOR_NODE(node))
       printf("functor(%s)\n", YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)( ~ApplTag & TrNode_entry(node)))));
     else if (YAP_IsIntTerm(TrNode_entry(node)))
-      printf("int(%ld)\n", YAP_IntOfTerm(TrNode_entry(node)));
+      printf("int(" Int_FORMAT ")\n", YAP_IntOfTerm(TrNode_entry(node)));
     else if (YAP_IsAtomTerm(TrNode_entry(node)))
        printf("atom(%s)\n", YAP_AtomName(YAP_AtomOfTerm(TrNode_entry(node))));
     else
@@ -816,7 +816,7 @@ int traverse_get_counter(TrNode node) {
 
 YAP_Term generate_label(YAP_Int Index) {
   char label[20];
-  sprintf(label,"L%ld", Index);
+  sprintf(label,"L" Int_FORMAT, Index);
   return YAP_MkAtomTerm(YAP_LookupAtom(label));
 }
 
diff --git a/library/tries/core_tries.c b/library/tries/core_tries.c
index 7540d8797..c3974a12b 100644
--- a/library/tries/core_tries.c
+++ b/library/tries/core_tries.c
@@ -1323,7 +1323,7 @@ void traverse_and_save(TrNode node, FILE *file, int float_block) {
     TrNode *first_bucket, *bucket;
     TrHash hash;
     hash = (TrHash) node;
-    fprintf(file, "%lu %d ", HASH_SAVE_MARK, TrHash_num_buckets(hash));
+    fprintf(file, UInt_FORMAT " %d ", HASH_SAVE_MARK, TrHash_num_buckets(hash));
     first_bucket = TrHash_buckets(hash);
     bucket = first_bucket + TrHash_num_buckets(hash);
     do {
@@ -1341,7 +1341,7 @@ void traverse_and_save(TrNode node, FILE *file, int float_block) {
   t = TrNode_entry(node);
   if (float_block) {
     float_block--;
-    fprintf(file, "%lu %lu ", FLOAT_SAVE_MARK, t);
+    fprintf(file, UInt_FORMAT " " UInt_FORMAT "  ", FLOAT_SAVE_MARK, t);
   } else if (YAP_IsPairTerm(t)) {
     if (t == FloatInitTag) {
 #ifdef TAG_LOW_BITS_32
@@ -1349,9 +1349,9 @@ void traverse_and_save(TrNode node, FILE *file, int float_block) {
 #endif /* TAG_LOW_BITS_32 */
       float_block ++;
     }
-    fprintf(file, "%lu ", t);
+    fprintf(file, UInt_FORMAT " ", t);
   } else if (YAP_IsVarTerm(t) || YAP_IsIntTerm(t))
-    fprintf(file, "%lu ", t);
+    fprintf(file, UInt_FORMAT" ", t);
   else {
     int index;
     for (index = 0; index <= CURRENT_INDEX; index++)
@@ -1363,16 +1363,16 @@ void traverse_and_save(TrNode node, FILE *file, int float_block) {
 	expand_auxiliary_term_stack();
       AUXILIARY_TERM_STACK[CURRENT_INDEX] = t;
       if (YAP_IsAtomTerm(t))
-	  fprintf(file, "%lu %d %s%c ", ATOM_SAVE_MARK, index, YAP_AtomName(YAP_AtomOfTerm(t)), '\0');
+	  fprintf(file, UInt_FORMAT " %d %s%c ", ATOM_SAVE_MARK, index, YAP_AtomName(YAP_AtomOfTerm(t)), '\0');
       else  /* (ApplTag & t) */
-	fprintf(file, "%lu %d %s %lu ", FUNCTOR_SAVE_MARK, index,
+	fprintf(file, UInt_FORMAT " %d %s " UInt_FORMAT " ", FUNCTOR_SAVE_MARK, index,
 		YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & t))),
 		YAP_ArityOfFunctor((YAP_Functor)(~ApplTag & t)));
     } else
       if (YAP_IsAtomTerm(t))
-	fprintf(file, "%lu %d ", ATOM_SAVE_MARK, index);
+	fprintf(file, UInt_FORMAT " %d ", ATOM_SAVE_MARK, index);
       else
-	fprintf(file, "%lu %d ", FUNCTOR_SAVE_MARK, index);
+	fprintf(file, UInt_FORMAT " %d ", FUNCTOR_SAVE_MARK, index);
   }
   if (IS_LEAF_TRIE_NODE(node)) {
     fprintf(file, "- ");
@@ -1393,7 +1393,7 @@ void traverse_and_load(TrNode parent, FILE *file) {
   YAP_Term t;
   int n;
 
-  if (!fscanf(file, "%lu", &t)) {
+  if (!fscanf(file, UInt_FORMAT , &t)) {
     MARK_AS_LEAF_TRIE_NODE(parent);
     INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE);
     if (DATA_LOAD_FUNCTION)
@@ -1407,7 +1407,7 @@ void traverse_and_load(TrNode parent, FILE *file) {
     n = fscanf(file, "%d", &num_buckets);
     new_trie_hash(hash, 0, num_buckets);
     TrNode_child(parent) = (TrNode) hash;
-    n = fscanf(file, "%lu", &t);
+    n = fscanf(file, UInt_FORMAT , &t);
   }
   do {
     TrNode child;
@@ -1446,10 +1446,10 @@ void traverse_and_load(TrNode parent, FILE *file) {
       }
       t = AUXILIARY_TERM_STACK[index];
     } else if (t == FLOAT_SAVE_MARK)
-      n = fscanf(file, "%lu", &t);
+      n = fscanf(file, UInt_FORMAT , &t);
     child = trie_node_insert(parent, t, hash);
     traverse_and_load(child, file);
-  } while (fscanf(file, "%lu", &t));
+  } while (fscanf(file, UInt_FORMAT , &t));
   CURRENT_DEPTH--;
   if (n) n = 0; // just added to remove the warning of not used!
   return;
@@ -1545,7 +1545,7 @@ void traverse_and_print(TrNode node, int *arity, char *str, int str_index, int m
     }
     mode = TRIE_PRINT_NORMAL;
   } else if (YAP_IsVarTerm(t)) {
-    str_index += sprintf(& str[str_index], "VAR%ld", TrieVarIndex(t));
+    str_index += sprintf(& str[str_index], "VAR" UInt_FORMAT, TrieVarIndex(t));
     while (arity[0]) {
       if (arity[arity[0]] == 1) {
 	str_index += sprintf(& str[str_index], ")");
@@ -1571,7 +1571,7 @@ void traverse_and_print(TrNode node, int *arity, char *str, int str_index, int m
       }
     }
   } else if (YAP_IsIntTerm(t)) {
-    str_index += sprintf(& str[str_index], "%ld", YAP_IntOfTerm(t));
+    str_index += sprintf(& str[str_index], UInt_FORMAT , YAP_IntOfTerm(t));
     while (arity[0]) {
       if (arity[arity[0]] == 1) {
 	str_index += sprintf(& str[str_index], ")");
diff --git a/library/tries/core_tries.h b/library/tries/core_tries.h
index 23b25b992..5608c907c 100644
--- a/library/tries/core_tries.h
+++ b/library/tries/core_tries.h
@@ -119,7 +119,7 @@ typedef struct trie_hash {
 #define SIZEOF_TR_HASH    sizeof(TYPE_TR_HASH)
 #define SIZEOF_TR_BUCKET  sizeof(TYPE_TR_NODE *)
 
-#define AS_TR_NODE_NEXT(ADDR) (TrNode)((unsigned long int)(ADDR) - 2 * sizeof(struct trie_node *))
+#define AS_TR_NODE_NEXT(ADDR) (TrNode)((YAP_UInt)(ADDR) - 2 * sizeof(struct trie_node *))
 
 
 
@@ -127,12 +127,12 @@ typedef struct trie_hash {
 /*           Macros            */
 /* --------------------------- */
 
-#define TAG_ADDR(ADDR)                            ((unsigned long int)(ADDR) | 0x1)
-#define UNTAG_ADDR(ADDR)                          ((unsigned long int)(ADDR) & ~(0x1))
+#define TAG_ADDR(ADDR)                            ((YAP_UInt)(ADDR) | 0x1)
+#define UNTAG_ADDR(ADDR)                          ((YAP_UInt)(ADDR) & ~(0x1))
 #define PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, DATA) TrNode_child(TR_NODE) = (TrNode)TAG_ADDR(DATA)
 #define GET_DATA_FROM_LEAF_TRIE_NODE(TR_NODE)     UNTAG_ADDR(TrNode_child(TR_NODE))
 #define MARK_AS_LEAF_TRIE_NODE(TR_NODE)           PUT_DATA_IN_LEAF_TRIE_NODE(TR_NODE, TrNode_child(TR_NODE))
-#define IS_LEAF_TRIE_NODE(TR_NODE)                ((unsigned long int)(TrNode_child(TR_NODE)) & 0x1)
+#define IS_LEAF_TRIE_NODE(TR_NODE)                ((YAP_UInt)(TrNode_child(TR_NODE)) & 0x1)
 
 #define IsTrieVar(TERM, STACK, STACK_BASE) ((YAP_Term *)(TERM) > STACK && (YAP_Term *)(TERM) <= STACK_BASE)
 #define MkTrieVar(INDEX)                   ((INDEX) << 4)
diff --git a/misc/mkwin b/misc/mkwin
index e5e4beb1d..1f7f75624 100755
--- a/misc/mkwin
+++ b/misc/mkwin
@@ -19,6 +19,7 @@
 # http://nsis.sourceforge.net/Main_Page
 #
 #
+YHOME=/y/vsc
 VERSION=6.3.4
 #cross-compiler for OSX, see http://mxe.cc/
 #notice that OSX does not allow WIN64 emulation (wine64)
@@ -27,9 +28,11 @@ MXE=$HOME/Yap/mxe/usr
 THREADS=no
 # use 64 bits
 ABI=64
+
+SRC=/c/cygwin/Yap/yap-6.3
 #SRC=/l/work/noth/git
 #SRC_WIN=L:\\work\\noth\\git
-SRC="$HOME"/git/yap-6.3
+#SRC="$HOME"/git/yap-6.3
 TARGET=/c/Yap64
 NSIS="/c/Program Files (x86)/NSIS/makensis"
 # by default, ""
@@ -57,7 +60,8 @@ do
 done
 
 # srcdir comes from here, please avoid relative paths
-CONFIGURE=$SRC/configure
+CONFIGURE="$SRC"/configure
+DEBUG=" --enable-debug-yap --enable-low-level-tracer"
 
 # debugging setup
 do_compile=true
@@ -66,54 +70,51 @@ do_install=true
 # HOME WIN64 configuration
 
 # DOCS_DIR=/l/work/noth/yapdocs
-DOCS_DIR=$HOME/Yap/bins/threads
+DOCS_DIR="$YHOME"/Yap/bins/threads
+
 if test "$THREADS" = yes; then
   FULL_VERSION="$VERSION"-threads
 else
   FULL_VERSION="$VERSION"
-fi
+fi                                                           
 
 if test $ABI = 64; then
-  #GCC_PATH=/l/Work/noth/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64
+  BUILD=/c/cygwin/Yap/mingw"$ABI"
+  #GCC_DIR=/l/Work/noth/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64
   case $( uname ) in
     *Darwin*)
-     GCC_PATH="$MXE"/bin
-     PATH=$PATH:$GCC_PATH
+     GCC_DIR="$MXE"
      HOST="x86_64-w64-mingw32"
      LIB_PATH="$MXE"/"$HOST"/lib
      ;;
-   esac
+    *MINGW64*)
+     GCC_DIR=/c/TDM-GCC-64
+     HOST="x86_64-w64-mingw32"
+     LIB_PATH="$GCC_DIR"/"$HOST"/lib
+     ;;
+  esac
   # ok.
-
-  CUDD=no # BDD compiler package. Get version that compiles on Windows from Vitor!
-  GECODE=no  #does not link with mingw64, VC++ trouble
-  GMP=yes    # Compile it for infinite precision numbers and rationals
-  JAVA=no    # for JPL only
-  PYTHON=no  # does not work in Windows anyway (currently)
-  REAL=no    # interface to R
-
-  # it seems python2.7 does not support mingw64
-  PYTHON_PATH="/c/Python33-64"
-  export PATH="$PYTHON_PATH"/bin:"$PATH"
-  GECODE_PATH="/c/Program Files/Gecode"
+  # BDD compiler package. Get version that compiles on Windows from Vitor!
   #  GMP=/l/Work/noth/msys/1.0/local
-  GMP=yes
-  CUDD_PATH=/c/cygwin/Yap/cudd-2.5.0-mingw64
-  JAVA_PATH="/c/Program Files/Java/jdk1.7.0_51"
-  R_PATH="/c/Program Files/R/R-3.1.0"
+  GMP=/c/msys64/usr/win64
+  CUDD=/c/cygwin/Yap/cudd-2.5.0-mingw64
+  GECODE=no  # "/c/Program Files/Gecode"
+  JAVA="$( echo /c/Program\ Files/Java/jdk* )"
+  PYTHON="/c/Python33-64"
+  R="$( echo /c/Program\ Files/R/R-*/bin/x64 )"
+
 fi
 
 # HOME WIN32 configuration
 if test $ABI = 32; then
+  ABI=32
   case $( uname ) in
     *Darwin*)
     #use mxe as a cross compiler
-    echo $( uname )
-    GCC_PATH="$MXE"/bin
-    PATH="$GCC_PATH":"$PATH"
+    GCC_DIR="$MXE"/bin
+    PATH="$GCC_DIR":"$PATH"
     HOST="i686-pc-mingw32"
     LIB_PATH="$MXE"/"$HOST"/lib
-    CUDD=yes
     GECODE=no # install only allows one of 32 or 64 bits
     GMP=yes
     JAVA=no
@@ -122,18 +123,48 @@ if test $ABI = 32; then
     ;;
   esac
 
-  PYTHON_PATH="/c/Python27"
-  export PATH="$GCC_PATH"/bin:$PATH
-  GECODE_PATH="/c/Program Files/Gecode"
-  GMP=/c/cygwin/Yap/win32
-  CUDD_PATH=/c/cygwin/Yap/cudd-2.5.0-mingw32
-  JAVA_PATH="/c/Program Files (x86)/Java/jdk1.7.0_51"
-  R_PATH="/c/Program Files/R/R-3.0.2"
+  case $( uname ) in
+    *Darwin*)
+     GCC_DIR="$MXE"
+     HOST="x86_64-w64-mingw32"
+     LIB_PATH="$MXE"/"$HOST"/lib
+     ;;
+    *MINGW64*)
+     GCC_DIR=/c/TDM-GCC-64
+     HOST="x86_64-w64-mingw32"
+     LIB_PATH="$GCC_DIR"/"$HOST"/lib
+     ;;
+  esac
+  # ok.
+  # BDD compiler package. Get version that compiles on Windows from Vitor!
+  #  GMP=/l/Work/noth/msys/1.0/local
+  GMP=/c/msys64/usr/win32
+  CUDD=/c/cygwin/Yap/cudd-2.5.0-mingw32
+  GECODE=no  # "/c/Program Files/Gecode"
+  JAVA="$( echo /c/Program\ Files\ */Java/jdk* )"
+  PYTHON="/c/Python27"
+  R="$( echo /c/Program\ Files/R/R-*/bin/i* )"
   HOST+=" --enable-abi=32"
 
 fi
 
-if test $THREADS = yes
+export PATH="$GCC_DIR"/bin:"$PATH"
+# echo "gcc= " $GCC_DIR
+# echo "host= " $HOST
+if test x"$JAVA" != xno
+then
+  export PATH="$PATH":"$JAVA"/bin
+fi
+if test x"$PYTHON" != xno
+then
+  export PATH="$PATH":"$PYTHON"/"python.exe"
+fi
+if test x"$R" != xno
+then
+    export PATH="$PATH":"$R"
+fi
+
+if test ${THREADS} = yes
 then
   cp "$LIB_PATH"/libwinpthread-1.dll .
   cp libwinpthread-1.dll pthreadGC2.dll
@@ -142,7 +173,6 @@ fi
 
 if test $CUDD != no
 then
-  CUDD="$CUDD_PATH"
   BDDLIB="yes"
   CPLINT="yes"
 else
@@ -150,44 +180,51 @@ else
   CPLINT="no"
 fi
 
-if test $GECODE = yes
+if test x"$GECODE" != xno
 then
-  export PATH="$PATH":"$GECODE_PATH"/bin
+  export PATH="$PATH":"$GECODE"/bin
 fi
 
-if test $JAVA = yes
+if test "$JAVA" = yes
 then
-  export PATH="$PATH":"$JAVA_PATH"/bin
+  export PATH="$PATH":"$JAVA"/bin
 fi
 
-if test $PYTHON = yes
+if test "$PYTHON" = yes
 then
-  export PATH="$PATH":"$PYTHON_PATH"
+  export PATH="$PATH":"$PYTHON"
 fi
 
-if test $REAL = yes
+if test x"$R"  != xno
 then
   if test $ABI = 32; then
     R_ABI=i386
   else
     R_ABI=x64
   fi
-  export PATH="$PATH":"$R_PATH"/bin/"$R_ABI"
+  export PATH="$PATH":"$R"
+fi
+
+if test do_compile = true
+then
+   make distclean
 fi
 
 export INSTALL_SH=$SRC/yap-6.3/install.sh
 # avoid using relative paths
 if test "$do_compile" = true; then
-  "$CONFIGURE" --host=$HOST \
-   --prefix=$TARGET \
-     --with-R="$REAL" \
+  BUILD=/c/cygwin/Yap/mingw"$ABI"
+  mkdir -p  "$BUILD"
+#  /bin/rm -rf "$BUILD"/*
+  "$CONFIGURE" --host="$HOST" \
+   --prefix="$TARGET"  $DEBUG\
+     --with-R="$R" \
        --with-java="$JAVA" \
          --with-gmp="$GMP" \
            --with-python="$PYTHON" \
              --with-cudd="$CUDD" --enable-bddlib="$BDDLIB" --with-cplint="$CPLINT" \
                --with-gecode="$GECODE" \
-                 --enable-threads="$THREADS"  --enable-pthread-locking \
-                   --enable-debug-yap --enable-low-level-tracer
+                 --enable-threads="$THREADS"  --enable-pthread-locking
 
  make #-j 4 install
 fi
@@ -199,7 +236,7 @@ if test "$do_install" = true; then
       -DROOTDIR=$TARGET \
         -DABI="$ABI" \
           -DVERSION="$FULL_VERSION" \
-            -DOPTIONS="$SRC_WIN\\yap-6.3\\misc\\options.ini" \
+            -DOPTIONS="$SRC_WIN\\misc\\options.ini" \
               -DOUT_DIR=".." -D"WIN64=1" \
                 -NOCD $SRC/yap-6.3/misc/Yap.nsi
   else
@@ -209,7 +246,7 @@ if test "$do_install" = true; then
       -DROOTDIR=$TARGET \
         -DABI="$ABI" \
           -DVERSION="$FULL_VERSION" \
-            -DOPTIONS="$SRC_WIN\\yap-6.3\\misc\\options.ini" \
+            -DOPTIONS="$SRC_WIN\\misc\\options.ini" \
               -DOUT_DIR=".." \
                 -NOCD $SRC/yap-6.3/misc/Yap.nsi
   fi
diff --git a/os/SWI-Stream.h b/os/SWI-Stream.h
index 81b8c70ad..9bc1442b7 100755
--- a/os/SWI-Stream.h
+++ b/os/SWI-Stream.h
@@ -46,11 +46,12 @@
 #include <wchar.h>
 #include <stddef.h>
 #ifdef __WINDOWS__
+#include <stdint.h>
 #ifndef INT64_T_DEFINED
 #define INT64_T_DEFINED 1
 typedef __int64 int64_t;
 typedef unsigned __int64 uint64_t;
-#if (_MSC_VER < 1300) && !defined(__MINGW32__)
+#if (_MSC_VER < 1300)
 typedef long intptr_t;
 typedef unsigned long uintptr_t;
 typedef intptr_t ssize_t;		/* signed version of size_t */
diff --git a/os/pl-stream.c b/os/pl-stream.c
index 7ee6ba97c..238a520f0 100755
--- a/os/pl-stream.c
+++ b/os/pl-stream.c
@@ -22,6 +22,7 @@
 */
 
 #if defined(__WINDOWS__) || defined(__MINGW32__)
+#include <stdint.h>
 #include "windows/uxnt.h"
 #ifdef _YAP_NOT_INSTALLED_
 #include <config.h>
diff --git a/os/windows/uxnt.c b/os/windows/uxnt.c
index b75a658bf..96072f221 100755
--- a/os/windows/uxnt.c
+++ b/os/windows/uxnt.c
@@ -789,7 +789,7 @@ simple:
 
 
 int
-_xos_chmod(const char *path, int mode)
+_xos_chmod(const char *path, mode_t mode)
 { TCHAR buf[PATH_MAX];
 
   if ( !_xos_os_filenameW(path, buf, PATH_MAX) )
@@ -1003,6 +1003,20 @@ _xos_getcwd(char *buf, size_t len)
   { return _xos_canonical_filenameW(buf1, buf, len, 0);
   }
 
+  return NULL;
+}
+
+
+char *
+_xos_getcwd_i(char *buf, int len)
+{ TCHAR buf0[PATH_MAX];
+  TCHAR buf1[PATH_MAX];
+
+  if ( _wgetcwd(buf0, sizeof(buf0)/sizeof(TCHAR)) &&
+       _xos_long_file_nameW(buf0, buf1, sizeof(buf0)/sizeof(TCHAR)) )
+  { return _xos_canonical_filenameW(buf1, buf, len, 0);
+  }
+
   return NULL;
 }
 
diff --git a/os/windows/uxnt.h b/os/windows/uxnt.h
index 5c11227fd..a13325259 100644
--- a/os/windows/uxnt.h
+++ b/os/windows/uxnt.h
@@ -36,8 +36,10 @@
 #include <sys/stat.h>
 #include <stdio.h>
 #include <io.h>
+#if HAVE_DIRECT_H
 #include <direct.h>
-#if !defined(__MINGW32__)
+#endif
+#if !defined(__MINGW32__) && !defined(__MSYS__)
 #if (_MSC_VER < 1300)
 typedef long intptr_t;
 typedef unsigned long uintptr_t;
@@ -81,13 +83,13 @@ typedef intptr_t ssize_t;		/* signed version of size_t */
 #define chdir _xos_chdir
 #define mkdir _xos_mkdir
 #define rmdir _xos_rmdir
-#define getcwd _xos_getcwd
+#define getcwd _xos_getcwd_i
 #define setenv _xos_setenv
 #define fopen(p, m) _xos_fopen(p, m)
 
 #endif /*_UXNT_KERNEL*/
 
-#ifndef __MINGW32__
+#if !defined(__MINGW32__) && !defined(__MSYS__)
 #define F_OK 00
 #define R_OK 04				/* access() fields */
 #define W_OK 06
@@ -101,6 +103,10 @@ typedef intptr_t ssize_t;		/* signed version of size_t */
 #define PATH_MAX 1024
 #endif
 
+#if __MSYS__
+#define _stati64 stat
+#endif
+
 #undef _xos_stat
 
 _export char *  _xos_home(void);
@@ -130,7 +136,7 @@ _export ssize_t	_xos_write(int handle, const void *buf, size_t size);
 _export long	_xos_lseek(int handle, long offset, int whence);
 _export long	_xos_tell(int handle);
 _export int	_xos_access(const char *path, int mode);
-_export int	_xos_chmod(const char *path, int mode);
+_export int	_xos_chmod(const char *path, mode_t mode);
 _export int	_xos_remove(const char *path);
 _export int	_xos_rename(const char *old, const char *newname);
 _export int	_xos_stat(const char *path, struct _stati64 *sbuf);
@@ -138,6 +144,7 @@ _export int	_xos_chdir(const char *path);
 _export int	_xos_mkdir(const char *path, int mode);
 _export int	_xos_rmdir(const char *path);
 _export char *	_xos_getcwd(char *buf, size_t len);
+_export char *	_xos_getcwd_i(char *buf, int len);
 _export int	_xos_errno(void);
 _export int	_xos_exists(const char *path, int flags);
 _export size_t  _xos_getenv(const char *name, char *buf, size_t buflen);
diff --git a/packages/cplint/Makefile.in b/packages/cplint/Makefile.in
index ccd92804a..e462a9417 100644
--- a/packages/cplint/Makefile.in
+++ b/packages/cplint/Makefile.in
@@ -6,6 +6,7 @@ prefix = @prefix@
 exec_prefix = ${prefix}
 ROOTDIR = $(prefix)
 EROOTDIR = ${prefix}
+abs_top_builddir = @abs_top_builddir@
 #
 # where the binary should be
 #