private void gameLoop() {
		
		long beforeTime, afterTime, timeSpent, sleepTime, overTime;
		overTime = 0;
		long totalOverTime = 0;
		
		gameStart = System.nanoTime();
		beforeTime = gameStart;
		
		while (sc.isRunning()) {
			sc.update();
			sc.paint();
			afterTime = System.nanoTime();
			timeSpent = afterTime - beforeTime;
			sleepTime = FRAME_PERIOD - timeSpent - overTime;
			if (sleepTime < 0) { // Updating took longer than the frame period
				totalOverTime -= sleepTime;
				overTime = 0;
			} else {
				try {
					Thread.sleep(sleepTime/1000000); // ns --> ms
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				overTime = System.nanoTime() - afterTime - sleepTime;
			}
			
			beforeTime = System.nanoTime();
			
			// Update the game without rendering if speed-up is required to fulfill fps-request
			skips = 0;
			while (totalOverTime > FRAME_PERIOD && skips < MAX_NUMBER_OF_SKIPS) {
				sc.update();
				totalOverTime -= FRAME_PERIOD;
				skips++;
			}
			
			updateStats();
		}
		System.out.println("Average FPS: "+averageFPS);
	}