diff --git a/mex/build/matlab/k_order_perturbation/Makefile.am b/mex/build/matlab/k_order_perturbation/Makefile.am
index 5c05812bde903860e24c46f894ac033692bf2ef6..8397ebf70a7b742f8a76ba5e968dacae63fd7930 100644
--- a/mex/build/matlab/k_order_perturbation/Makefile.am
+++ b/mex/build/matlab/k_order_perturbation/Makefile.am
@@ -1,4 +1,4 @@
 include ../mex.am
 include ../../k_order_perturbation.am
 
-CPPFLAGS += -DMEXEXT=\"$(MEXEXT)\"
+CPPFLAGS += -DMEXEXT=$(MEXEXT)
diff --git a/mex/build/octave/k_order_perturbation/Makefile.am b/mex/build/octave/k_order_perturbation/Makefile.am
index 2b3f22e06c312235e2cb9f57f4f6039e6796fb33..ded6c99407151febf25f57653c991a2533ae2783 100644
--- a/mex/build/octave/k_order_perturbation/Makefile.am
+++ b/mex/build/octave/k_order_perturbation/Makefile.am
@@ -1,4 +1,4 @@
 include ../mex.am
 include ../../k_order_perturbation.am
 
-CPPFLAGS += -DMEXEXT=\"mex\"
+CPPFLAGS += -DMEXEXT=mex
diff --git a/mex/sources/build_matlab.m b/mex/sources/build_matlab.m
index 9d8f36d959dcb692bf3c9f591da72fc7dcbaabee..fa31e65e7fb1635ded7330c1dbb7ceddd34614b3 100644
--- a/mex/sources/build_matlab.m
+++ b/mex/sources/build_matlab.m
@@ -32,6 +32,7 @@ if strcmpi('GLNX86', computer) || strcmpi('GLNXA64', computer) ...
     else
         BLAS_PATH = '-lmwblas';
     end
+    DL_PATH = '-ldl';
 elseif strcmpi('PCWIN', computer) || strcmpi('PCWIN64', computer)
     % Windows (x86-32 or x86-64) with Microsoft or gcc compiler
     if strcmpi('PCWIN', computer)
@@ -45,6 +46,7 @@ elseif strcmpi('PCWIN', computer) || strcmpi('PCWIN64', computer)
     else
         BLAS_PATH = ['"' LIBRARY_PATH 'libmwblas.lib"'];
     end
+    DL_PATH = '';
 else
     error('Unsupported platform')
 end
@@ -90,7 +92,7 @@ LDDEBUGFLAGS = ' LDDEBUGFLAGS= ';
 COMPILE_OPTIONS = [ COMPILE_OPTIONS CDEBUGFLAGS COPTIMFLAGS CXXDEBUGFLAGS CXXOPTIMFLAGS LDDEBUGFLAGS LDOPTIMFLAGS];
 
 % Comment next line to suppress compilation debugging info
-% COMPILE_OPTIONS = [ COMPILE_OPTIONS ' -v' ];
+COMPILE_OPTIONS = [ COMPILE_OPTIONS ' -v' ];
 
 COMPILE_COMMAND = [ 'mex ' COMPILE_OPTIONS ' -outdir ' OUTPUT_DIR ];
 
@@ -126,4 +128,70 @@ eval([ COMPILE_COMMAND ' -I. -I../../dynare++/sylv/cc ' ...
        BLAS_PATH ' ' LAPACK_PATH ]);
 
 disp('Compiling bytecode...')
-eval([ COMPILE_COMMAND ' -Ibytecode -I../../preprocessor bytecode/bytecode.cc bytecode/Interpreter.cc bytecode/Mem_Mngr.cc bytecode/SparseMatrix.cc']);
\ No newline at end of file
+eval([ COMPILE_COMMAND ' -Ibytecode -I../../preprocessor bytecode/bytecode.cc bytecode/Interpreter.cc bytecode/Mem_Mngr.cc bytecode/SparseMatrix.cc']);
+
+disp('Compiling k_order_perturbation')
+eval([ COMPILE_COMMAND ...
+       ' -I../../dynare++/src -I../../dynare++/kord -I../../dynare++/tl/cc -I../../dynare++/utils/cc -I../../dynare++/sylv/cc -I../../dynare++/integ/cc -I. ' ...
+       ' -DMEXEXT=' mexext ' ' ...
+       '-output k_order_perturbation ' ...
+       '../../dynare++/kord/faa_di_bruno.cpp ' ...
+       '../../dynare++/kord/korder_stoch.cpp ' ...
+       '../../dynare++/kord/journal.cpp ' ...
+       '../../dynare++/kord/decision_rule.cpp ' ...
+       '../../dynare++/kord/dynamic_model.cpp ' ...
+       '../../dynare++/kord/random.cpp ' ...
+       '../../dynare++/kord/first_order.cpp ' ...
+       '../../dynare++/kord/normal_conjugate.cpp ' ...
+       '../../dynare++/kord/approximation.cpp ' ...
+       '../../dynare++/kord/global_check.cpp ' ...
+       '../../dynare++/kord/korder.cpp ' ...
+       '../../dynare++/sylv/cc/IterativeSylvester.cpp ' ...
+       '../../dynare++/sylv/cc/QuasiTriangular.cpp ' ...
+       '../../dynare++/sylv/cc/QuasiTriangularZero.cpp ' ...
+       '../../dynare++/sylv/cc/GeneralMatrix.cpp ' ...
+       '../../dynare++/sylv/cc/GeneralSylvester.cpp ' ...
+       '../../dynare++/sylv/cc/SimilarityDecomp.cpp ' ...
+       '../../dynare++/sylv/cc/SylvException.cpp ' ...
+       '../../dynare++/sylv/cc/SchurDecompEig.cpp ' ...
+       '../../dynare++/sylv/cc/Vector.cpp ' ...
+       '../../dynare++/sylv/cc/TriangularSylvester.cpp ' ...
+       '../../dynare++/sylv/cc/SylvParams.cpp ' ...
+       '../../dynare++/sylv/cc/BlockDiagonal.cpp ' ...
+       '../../dynare++/sylv/cc/KronVector.cpp ' ...
+       '../../dynare++/sylv/cc/SylvMemory.cpp ' ...
+       '../../dynare++/sylv/cc/SymSchurDecomp.cpp ' ...
+       '../../dynare++/sylv/cc/SylvMatrix.cpp ' ...
+       '../../dynare++/sylv/cc/SchurDecomp.cpp ' ...
+       '../../dynare++/sylv/cc/KronUtils.cpp ' ...
+       '../../dynare++/tl/cc/normal_moments.cpp ' ...
+       '../../dynare++/tl/cc/int_sequence.cpp ' ...
+       '../../dynare++/tl/cc/tensor.cpp ' ...
+       '../../dynare++/tl/cc/ps_tensor.cpp ' ...
+       '../../dynare++/tl/cc/pyramid_prod2.cpp ' ...
+       '../../dynare++/tl/cc/equivalence.cpp ' ...
+       '../../dynare++/tl/cc/fine_container.cpp ' ...
+       '../../dynare++/tl/cc/kron_prod.cpp ' ...
+       '../../dynare++/tl/cc/t_polynomial.cpp ' ...
+       '../../dynare++/tl/cc/symmetry.cpp ' ...
+       '../../dynare++/tl/cc/stack_container.cpp ' ...
+       '../../dynare++/tl/cc/twod_matrix.cpp ' ...
+       '../../dynare++/tl/cc/sparse_tensor.cpp ' ...
+       '../../dynare++/tl/cc/sthread.cpp ' ...
+       '../../dynare++/tl/cc/gs_tensor.cpp ' ...
+       '../../dynare++/tl/cc/pyramid_prod.cpp ' ...
+       '../../dynare++/tl/cc/fs_tensor.cpp ' ...
+       '../../dynare++/tl/cc/permutation.cpp ' ...
+       '../../dynare++/tl/cc/rfs_tensor.cpp ' ...
+       '../../dynare++/tl/cc/t_container.cpp ' ...
+       '../../dynare++/tl/cc/tl_static.cpp ' ...
+       '../../dynare++/integ/cc/product.cpp ' ...
+       '../../dynare++/integ/cc/quadrature.cpp ' ...
+       '../../dynare++/integ/cc/quasi_mcarlo.cpp ' ...
+       '../../dynare++/integ/cc/smolyak.cpp ' ...
+       '../../dynare++/integ/cc/vector_function.cpp ' ...
+       'k_order_perturbation/k_order_perturbation.cpp ' ...
+       'k_order_perturbation/k_ord_dynare.cpp ' ...
+       'k_order_perturbation/dynamic_dll.cpp ' ...
+       '../../dynare++/src/nlsolve.cpp ' ...
+       DL_PATH ' ' BLAS_PATH ' ' LAPACK_PATH]);
diff --git a/mex/sources/build_octave.m b/mex/sources/build_octave.m
index cec8b13ac557fbc76c9e13785a8f17010365cf29..ccb70d8f71029ec25207a20ba0d529f89473f33f 100644
--- a/mex/sources/build_octave.m
+++ b/mex/sources/build_octave.m
@@ -1,4 +1,7 @@
 % Build file for Dynare MEX Librairies for Octave
+%
+% This script is now obsolete and is no longer maintained.
+% Please use the autoconf/automake build system instead.
 
 % Copyright (C) 2008-2009 Dynare Team
 %
diff --git a/mex/sources/k_order_perturbation/dynamic_dll.cpp b/mex/sources/k_order_perturbation/dynamic_dll.cpp
index 74b4759e800a66835c0c0acc1dd41413853a765c..e15b71891ee1878b17aa9ef738b308d9fb418bd1 100644
--- a/mex/sources/k_order_perturbation/dynamic_dll.cpp
+++ b/mex/sources/k_order_perturbation/dynamic_dll.cpp
@@ -90,8 +90,9 @@ DynamicModelDLL::DynamicModelDLL(const char *modName, const int y_length, const
 
   try
     {
+#define QUOTEME(x) #x
       if (sExt == NULL)
-        sExt = MEXEXT;
+        sExt = QUOTEME(MEXEXT);
 #ifdef _WIN32
       HINSTANCE dynamicHinstance;
       //		dynamicHinstance=::LoadLibraryEx(strcat(fNname,"_.dll"),NULL,DONT_RESOLVE_DLL_REFERENCES);//sExt); //"_.dll");