 
         /** Store the manifest data. **/
         private function _manifestLoadedHandler(event:HLSEvent):void {
-            _levels = event.levels;
-            _initlevelswitch();
+            var levels:Array= event.levels;
+            var maxswitchup:Number=0;
+            var minswitchdwown:Number=Number.MAX_VALUE;
+            _nbLevel = levels.length;
+            _bitrate = new Array(_nbLevel);
+            _switchup = new Array(_nbLevel);
+            _switchdown = new Array(_nbLevel);
+            var i:Number;
+
+            for(i=0 ; i < _nbLevel; i++) {
+              _bitrate[i] = levels[i].bitrate;
+            }
+
+            // Select the lowest non-audio level.
+            for(i = 0; i < _nbLevel; i++) {
+                if(!levels[i].audio) {
+                    _lowestNonAudioLevel = i;
+                    break;
+                }
+            }
+
+            for(i=0 ; i < _nbLevel-1; i++) {
+               _switchup[i] = (_bitrate[i+1] - _bitrate[i]) / _bitrate[i];
+               maxswitchup = Math.max(maxswitchup,_switchup[i]);
+            }
+            for(i=0 ; i < _nbLevel-1; i++) {
+               _switchup[i] = Math.min(maxswitchup,2*_switchup[i]);
+               //Log.txt("_switchup["+i+"]="+_switchup[i]);
+            }
+
+            for(i = 1; i < _nbLevel; i++) {
+               _switchdown[i] = (_bitrate[i] - _bitrate[i-1]) / _bitrate[i];
+               minswitchdwown  =Math.min(minswitchdwown,_switchdown[i]);
+            }
+            for(i = 1; i < _nbLevel; i++) {
+               _switchdown[i] = Math.max(2*minswitchdwown,_switchdown[i]);
+               //Log.txt("_switchdown["+i+"]="+_switchdown[i]);
+            }
         };
-
-        /* initialize level switching heuristic tables */
-        private function _initlevelswitch():void {
-          var i:Number;
-          var maxswitchup:Number=0;
-          var minswitchdwown:Number=Number.MAX_VALUE;
-          _switchup = new Array(_levels.length);
-          _switchdown = new Array(_levels.length);
-
-          for(i=0 ; i < _levels.length-1; i++) {
-             _switchup[i] = (_levels[i+1].bitrate - _levels[i].bitrate) / _levels[i].bitrate;
-             maxswitchup = Math.max(maxswitchup,_switchup[i]);
-          }
-          for(i=0 ; i < _levels.length-1; i++) {
-             _switchup[i] = Math.min(maxswitchup,2*_switchup[i]);
-             //Log.txt("_switchup["+i+"]="+_switchup[i]);
-          }
-
-
-          for(i = 1; i < _levels.length; i++) {
-             _switchdown[i] = (_levels[i].bitrate - _levels[i-1].bitrate) / _levels[i].bitrate;
-             minswitchdwown  =Math.min(minswitchdwown,_switchdown[i]);
-          }
-          for(i = 1; i < _levels.length; i++) {
-             _switchdown[i] = Math.max(2*minswitchdwown,_switchdown[i]);
-             //Log.txt("_switchdown["+i+"]="+_switchdown[i]);
-          }
-        }
 
         /** Update the quality level for the next fragment load. **/
         public function getnextlevel(current_level:Number, buffer:Number, last_segment_duration:Number, last_fetch_duration:Number, last_bandwidth:Number):Number {
-         var i:Number;
-
-            var level:Number = -1;
-            // Select the lowest non-audio level.
-            for(i = 0; i < _levels.length; i++) {
-                if(!_levels[i].audio) {
-                    level = i;
-                    break;
-                }
-            }
+            var i:Number;
+            var level:Number = _lowestNonAudioLevel;
             if(level == -1) {
                 Log.txt("No other quality levels are available");
                 return -1;

               or buffer time is too small to retrieve one fragment with current level
            */

            else if(current_level > 0 &&((fetchratio < (1-_switchdown[current_level])) || (bufferratio < 1)) ) {
                  //Log.txt("bufferratio < 2 || fetchratio: < 1-_switchdown[_level]="+(1-_switchdown[_level]));
                  /* find suitable level matching current bandwidth, starting from current level
                     when switching level down, we also need to consider that we might need to load two fragments.

               the condition (bufferratio > 2*_levels[_level+1].bitrate/_last_bandwidth)
               ensures that buffer time is bigger than than the time to download 2 fragments from current_level+1, if we keep same bandwidth
            */
            if((current_level < _nbLevel-1) && (fetchratio > (1+_switchup[current_level])) && (bufferratio > 2*_bitrate[current_level+1]/last_bandwidth)) {
               //Log.txt("fetchratio:> 1+_switchup[_level]="+(1+_switchup[current_level]));
               //Log.txt("switch to level " + (current_level+1));
                  //level up
