             for(int i = 0; i <= wId; i++) {
                 if(windows[i]->uiID == wId) {
                     window = windows[i];
-                    CopyAndDraw(in, window);
-                    ret = window->uiID;
+                    ret = CopyAndDraw(in, window);
                     break;
                 }
             }
-            if(ret == -1)
-                AF_ERROR("Invalide Window ID", AF_ERR_INVALID_ARG);
-        }
-
+            if(ret == -1) {
+                if(std::find(closedWindows.begin(), closedWindows.end(), wId) != closedWindows.end())
+                    return -2;
+                else
+                    AF_ERROR("Invalid Window ID", AF_ERR_INVALID_ARG);
+            }
+        }
         return ret;
     }
 }


    void MakeContextCurrent(WindowHandle wh)
    {
        if (wh != NULL)
        {
            glfwMakeContextCurrent(wh->pWindow);
            current = wh;
        }
    }

    void Draw()
    {
        // Safety check
        //MakeContextCurrent(current);

        // load texture from PBO
        glBindTexture(GL_TEXTURE_2D, current->gl_Tex);

        cudaGraphicsUnregisterResource(window->cudaPBOResource);

        // Delete memory
        closedWindows.push_back(window->uiID);

        delete window;
        windows.erase(std::find(windows.begin(), windows.end(), window));
    }
