diff --git a/wmake/src/wmkdep.l b/wmake/src/wmkdep.l
index 3b69732..d1e6917 100644
--- a/wmake/src/wmkdep.l
+++ b/wmake/src/wmkdep.l
@@ -147,6 +147,13 @@ struct HashEntry* visitedFiles[HASH_TABLE_SIZE];
 struct HashEntry* visitedDirs[HASH_TABLE_SIZE];
 
 
+int currentBuffer = 0;                     /* Buffer pointer stack counter */
+YY_BUFFER_STATE buffers[FILE_STACK_SIZE];  /* Buffer pointer stack */
+
+/* Directory path for the file loaded on the respective buffer pointer stack */
+char* buffers_dirpath[FILE_STACK_SIZE];
+
+
 int main(int argc, char* argv[])
 {
     char *basePos, *dotPos;
@@ -248,6 +255,9 @@ int main(int argc, char* argv[])
         free(objectFile);
     }
 
+    /* Just in case, avoid memory garbage */
+    buffers_dirpath[currentBuffer] = NULL;
+
     nextFile(sourceFile);
     yylex();
 
@@ -265,10 +275,6 @@ int main(int argc, char* argv[])
 }
 
 
-int currentBuffer = 0;                     /* Buffer pointer stack counter */
-YY_BUFFER_STATE buffers[FILE_STACK_SIZE];  /* Buffer pointer stack */
-
-
 /*
  * Add a directory name to the file name
  */
@@ -313,6 +319,28 @@ void nextFile(const char* fileName)
     /* Pointer to new file which is set if the file is successfully opened */
     FILE* newyyin = NULL;
 
+    /* First check if the file is on the same path as the file on the current buffer */
+    if(buffers_dirpath[currentBuffer]!=NULL)
+    {
+        char* pathName = addDirectoryName(buffers_dirpath[currentBuffer], fileName);
+
+        if ((newyyin = fopen(pathName, "r")))
+        {
+            printf("%s: %s\n", depFile, pathName);
+            buffers[currentBuffer++] = YY_CURRENT_BUFFER;
+            yy_switch_to_buffer(yy_create_buffer(newyyin, YY_BUF_SIZE));
+
+            buffers_dirpath[currentBuffer] = buffers_dirpath[currentBuffer-1];
+
+            free(pathName);
+
+            return;
+        }
+
+        free(pathName);
+    }
+
+    
     if (!(newyyin = fopen(fileName, "r")))
     {
         int d;
@@ -326,6 +354,8 @@ void nextFile(const char* fileName)
                 buffers[currentBuffer++] = YY_CURRENT_BUFFER;
                 yy_switch_to_buffer(yy_create_buffer(newyyin, YY_BUF_SIZE));
 
+                buffers_dirpath[currentBuffer] = directories[d];
+                
                 free(pathName);
 
                 return;
@@ -366,6 +396,8 @@ void nextFile(const char* fileName)
 
         buffers[currentBuffer++] = YY_CURRENT_BUFFER;
         yy_switch_to_buffer(yy_create_buffer(newyyin, YY_BUF_SIZE));
+
+        buffers_dirpath[currentBuffer] = NULL;
     }
 }
 
diff --git a/wmake/src/wmkdepend.cpp b/wmake/src/wmkdepend.cpp
index e4fe46c..445fce9 100644
--- a/wmake/src/wmkdepend.cpp
+++ b/wmake/src/wmkdepend.cpp
@@ -166,7 +166,7 @@ int main(int argc, char* argv[])
     wmake::Parser::sourceFile = sourceFile;
     wmake::Parser::depFile = depFile;
 
-    wmake::Parser::includeFile(sourceFile);
+    wmake::Parser::includeFile(sourceFile, std::string());
 
     return 0;
 }
diff --git a/wmake/src/wmkdependParser.cpp b/wmake/src/wmkdependParser.cpp
index 0984a56..0c935a0 100644
--- a/wmake/src/wmkdependParser.cpp
+++ b/wmake/src/wmkdependParser.cpp
@@ -77,7 +77,7 @@ void Parser::ignoreDir(const std::string& name)
 }
 
 
-void Parser::includeFile(const std::string& name)
+void Parser::includeFile(const std::string& name, const std::string& dirPath)
 {
     if (!visitedFiles.insert(name).second)
     {
@@ -85,10 +85,26 @@ void Parser::includeFile(const std::string& name)
     }
 
     // use stdio and buffering within Coco/R -- (faster)
-    FILE *fh = fopen(name.c_str(), "r");
+    FILE *fh = NULL;
+    std::string newReferenceDirPath;
+    std::string actualName = name;
+    
+    if(!dirPath.empty())
+    {
+        const std::string pathName = dirPath + name;
+
+        fh = fopen(pathName.c_str(), "r");
+        newReferenceDirPath = dirPath;
+        actualName = pathName;
+    }
+    else
+    {
+        fh = fopen(name.c_str(), "r");
+    }
+    
     if (fh)
     {
-        std::cout << depFile << ": " << name << "\n";
+        std::cout << depFile << ": " << actualName << "\n";
     }
     else
     {
@@ -105,6 +121,7 @@ void Parser::includeFile(const std::string& name)
             if (fh)
             {
                 std::cout << depFile << ": " << pathName << "\n";
+                newReferenceDirPath = *iter;
                 break;
             }
         }
@@ -115,7 +132,7 @@ void Parser::includeFile(const std::string& name)
         Scanner scanner(fh);
         Parser  parser(&scanner);
 
-        parser.Parse();
+        parser.Parse(newReferenceDirPath);
         fclose(fh);
     }
     else
@@ -153,7 +170,7 @@ void Parser::importFile(const std::string& name)
     dotToSlash(javaFileName);
     javaFileName += ".java";
 
-    includeFile(javaFileName);
+    includeFile(javaFileName, std::string());
 }
 
 
@@ -182,7 +199,7 @@ void Parser::importDir(const std::string& name)
             if (ext && strlen(ext) == 5)
             {
                 std::string pathName = dirName + list->d_name;
-                includeFile(pathName);
+                includeFile(pathName, dirName);
             }
         }
 
@@ -341,11 +358,11 @@ void Parser::wmkdepend()
 					Get();
 					if (isUTF8())
 					{
-					    includeFile(t->toStringUTF8(1, t->length()-2));
+					    includeFile(t->toStringUTF8(1, t->length()-2), bufferedPath);
 					}
 					else
 					{
-					    includeFile(t->toString(1, t->length()-2));
+					    includeFile(t->toString(1, t->length()-2), bufferedPath);
 					}
 
 				}
@@ -363,11 +380,11 @@ void Parser::wmkdepend()
 				Get();
 				if (isUTF8())
 				{
-				    includeFile(t->toStringUTF8(1, t->length()-2));
+				    includeFile(t->toStringUTF8(1, t->length()-2), bufferedPath);
 				}
 				else
 				{
-				    includeFile(t->toString(1, t->length()-2));
+				    includeFile(t->toString(1, t->length()-2), bufferedPath);
 				}
 
 			}
@@ -425,8 +442,11 @@ void Parser::wmkdepend()
 
 
 
-void Parser::Parse()
+void Parser::Parse(const std::string& refDirPath)
 {
+    //keep record of the first reference directory path
+    bufferedPath = refDirPath;
+
 	t = NULL;
 	// might call Parse() twice
 	if (dummyToken) {
@@ -446,6 +466,7 @@ Parser::Parser(Scanner* scan, Errors* err)
 	dummyToken(NULL),
 	deleteErrorsDestruct_(!err),
 	errDist(minErrDist),
+	bufferedPath(),
 	scanner(scan),
 	errors(err),
 	t(NULL),
diff --git a/wmake/src/wmkdependParser.h b/wmake/src/wmkdependParser.h
index 2285efa..d137378 100644
--- a/wmake/src/wmkdependParser.h
+++ b/wmake/src/wmkdependParser.h
@@ -104,6 +104,9 @@ class Parser
 	Token *dummyToken;
 	bool deleteErrorsDestruct_; //!< delete the 'errors' member in destructor
 	int  errDist;
+  
+    //! path for the directory where the buffered file is located
+    std::string bufferedPath;
 
 	void SynErr(int n);         //!< Handle syntax error 'n'
 	void Get();
@@ -150,7 +153,8 @@ public:
     static void ignoreDir(const std::string& name);
 
     //! Include file
-    static void includeFile(const std::string& name);
+    static void includeFile(const std::string& name,
+                            const std::string& dirPath);
 
 /*---------------------------------------------------------------------------*/
 
@@ -161,7 +165,7 @@ public:
 	 */
 	Parser(Scanner* scan, Errors* err = 0);
 	~Parser();
-	void Parse();                          //!< Execute the parse operation
+	void Parse(const std::string& refDirPath); //!< Execute the parse operation
 	void SemErr(const std::wstring& msg);  //!< Handle semantic error
 	bool isUTF8() const;   //!< Return true if scanner buffer is UTF8
 
