@Override
    public void run(){
        long beforeTime, afterTime, diff, sleepTime, overSleepTime = 0;
        int delays = 0;
        while(running){
            beforeTime = System.nanoTime();
            
            gameUpdate();
            gameRender();
            paintScreen();
            s.updateBullets();
            
            afterTime = System.nanoTime();
            diff = afterTime - beforeTime;
            sleepTime = (period - diff) - overSleepTime;
            // If the sleep time is between 0 and the period, we can happily sleep
            if(sleepTime < period && sleepTime > 0){
                try{
                    game.sleep(sleepTime / 1000000L);
                    overSleepTime = 0;
                } catch (InterruptedException ex) {
                    Logger.getLogger(GamePanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // The diff was greater than the period
            else if(diff > period){
                overSleepTime = diff - period;
            }
            // Accumulate the amount of delays, and eventually yeild
            else if(++delays >= DELAYS_BEFORE_YEILD){
                game.yield();
                delays = 0;
                overSleepTime = 0;
            }
            // The loop took less time than expected, but we need to make up
            // for overSleepTime
            else{
                overSleepTime = 0;
            }
//            // Print out game stats
//            log(
//                    "beforeTime:    " + beforeTime + "\n" +
//                    "afterTime:     " + afterTime + "\n" +
//                    "diff:          " + diff + "\n" +
//                    "sleepTime:     " + sleepTime / 1000000L + "\n" +
//                    "overSleepTime: " + overSleepTime / 1000000L + "\n" +
//                    "delays:        " + delays + "\n"
//            );
        }
    }