#include <sstream>
 #include <cmath>
 
-#include "Parser.hpp"
-
+#include "Code.hpp"
 #include "Decode.hpp"
 #include "Error.hpp"
 #include "JavaScript.hpp"
 extern "C" bool CydgetMemoryParse(const uint16_t **data, size_t *size);
 
 void *CYMapFile(const char *path, size_t *psize) {
-    int fd;
-    _syscall(fd = open(path, O_RDONLY));
+    int fd(_syscall_(open(path, O_RDONLY), 1, {ENOENT}));
+    if (fd == -1)
+        return NULL;
 
     struct stat stat;
     _syscall(fstat(fd, &stat));
     CYJSString property("exports");
     JSObjectRef module;
 
-    const char *path(pool.strcat(lib, "/cycript0.9/", CYPoolCString(pool, context, arguments[0]), ".cy", NULL));
+    const char *name(CYPoolCString(pool, context, arguments[0]));
+    const char *path(pool.strcat(lib, "/cycript0.9/", name, ".cy", NULL));
+
     CYJSString key(path);
     JSObjectRef modules(CYGetCachedObject(context, CYJSString("modules")));
     JSValueRef cache(CYGetProperty(context, modules, key));
     if (!JSValueIsUndefined(context, cache))
         module = CYCastJSObject(context, cache);
     else {
+        CYUTF8String code;
+        code.data = reinterpret_cast<char *>(CYMapFile(path, &code.size));
+
+        if (code.data == NULL) {
+            if (strchr(name, '/') == NULL && (
+                dlopen(pool.strcat("/System/Library/Frameworks/", name, ".framework/", name, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL ||
+                dlopen(pool.strcat("/System/Library/PrivateFrameworks/", name, ".framework/", name, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL ||
+            false))
+                return CYJSUndefined(NULL);
+
+            CYThrow("Can't find module: %s", name);
+        }
+
         module = JSObjectMake(context, NULL, NULL);
         CYSetProperty(context, modules, key, module);
 
         JSObjectRef exports(JSObjectMake(context, NULL, NULL));
         CYSetProperty(context, module, property, exports);
 
-        CYUTF8String code;
-        code.data = reinterpret_cast<char *>(CYMapFile(path, &code.size));
-
         std::stringstream wrap;
         wrap << "(function (exports, require, module) { " << code << "\n});";
-        code = CYPoolCode(pool, wrap.str().c_str());
+        code = CYPoolCode(pool, wrap);
 
         JSValueRef value(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0));
         JSObjectRef function(CYCastJSObject(context, value));