private void doEventLoop()
    {
        int currentStackLength = windowStack.size();
        if(currentStackLength == 0)
            return;

        while(true) {
            if(currentStackLength > windowStack.size()) {
                //The window was removed from the stack ( = it was closed)
                break;
            }

            synchronized(actionToRunInEventThread) {
                List<Action> actions = new ArrayList<Action>(actionToRunInEventThread);
                actionToRunInEventThread.clear();
                for(Action nextAction: actions)
                    nextAction.doAction();
            }

            boolean repainted = update();

            Key nextKey = screen.readInput();
            if(nextKey != null) {
                windowStack.getLast().window.onKeyPressed(nextKey);
                invalidate();
            }
            else {
                if(!repainted) {
                    try {
                        Thread.sleep(1);
                    }
                    catch(InterruptedException e) {}
                }
            }
        }
    }