                 } else {
                     // if previous segment is not the last one, increment it to get new seqnum
                     new_seqnum = last_seqnum + 1;
-                    if (new_seqnum < _levels[_level].start_seqnum) {
+                    if (new_seqnum < _levels[level].start_seqnum) {
                         // we are late ! report to caller
                         return -1;
                     }
-                    frag = _levels[_level].getFragmentfromSeqNum(new_seqnum);
+                    frag = _levels[level].getFragmentfromSeqNum(new_seqnum);
                     if (frag == null) {
                         CONFIG::LOGGING {
-                            Log.warn("error trying to load " + new_seqnum + " of [" + (_levels[_level].start_seqnum) + "," + (_levels[_level].end_seqnum) + "],level " + _level);
+                            Log.warn("error trying to load " + new_seqnum + " of [" + (_levels[level].start_seqnum) + "," + (_levels[level].end_seqnum) + "],level " + level);
                         }
                         return 1;
                     }
                     // check whether there is a discontinuity between last segment and new segment
-                    _hasDiscontinuity = (frag.continuity != _frag_previous.continuity);
+                    _hasDiscontinuity = (frag.continuity != frag_previous.continuity);
                     ;
                     log_prefix = "Loading       ";
                 }
             }
             _seqnum = new_seqnum;
-            frag = _levels[_level].getFragmentfromSeqNum(_seqnum);
-            CONFIG::LOGGING {
-                Log.debug(log_prefix + _seqnum + " of [" + (_levels[_level].start_seqnum) + "," + (_levels[_level].end_seqnum) + "],level " + _level);
+            frag = _levels[level].getFragmentfromSeqNum(_seqnum);
+            CONFIG::LOGGING {
+                Log.debug(log_prefix + _seqnum + " of [" + (_levels[level].start_seqnum) + "," + (_levels[level].end_seqnum) + "],level " + level);
             }
             _loadfragment(frag);
             return 0;

            }
        };

        private function _updateLevel(buffer : Number) : int {
            var level : int;
            if (_manifest_just_loaded) {
                level = _hls.startlevel;
            } else if (_fragment_first_loaded == false) {
                level = _hls.seeklevel;
            } else if (_bIOError == true) {
                /* in case IO Error has been raised, stick to same level */
                level = _level;
                /* in case fragment was loaded for PTS analysis, stick to same level */
            } else if (_pts_just_loaded == true) {
                _pts_just_loaded = false;
                level = _level;
                /* in case we are switching levels (waiting for playlist to reload) or seeking , stick to same level */
            } else if (_switchlevel == true) {
                level = _level;
            } else if (_manual_level == -1 && _levels.length > 1 ) {
                if (_metrics_previous) {
                    var last_segment_duration : Number = (_frag_previous ? 1000 * _frag_previous.duration : 0);
                    level = _autoLevelManager.getnextlevel(_level, buffer, last_segment_duration, _metrics_previous.processing_duration, _metrics_previous.bandwidth);
                } else {
                    level = _autoLevelManager.getnextlevel(_level, buffer, 0, 0, 0);
                }
            } else if (_manual_level == -1 && _levels.length == 1 ) {
                level = 0;
            } else {
                level = _manual_level;
            }
            if (level != _level || _manifest_just_loaded) {
                _level = level;
                _switchlevel = true;
                _hls.dispatchEvent(new HLSEvent(HLSEvent.LEVEL_SWITCH, _level));
            }
            return level;
        }

        private function _loadfirstfragment(position : Number) : int {
            CONFIG::LOGGING {
                Log.debug("loadfirstfragment(" + position + ")");
            }
            _need_reload = false;
            _switchlevel = true;
            _updateLevel(0);

            // check if we received playlist for new level. if live playlist, ensure that new playlist has been refreshed
            if ((_levels[_level].fragments.length == 0) || (_hls.type == HLSTypes.LIVE && _last_updated_level != _level)) {
                // playlist not yet received
                CONFIG::LOGGING {
                    Log.debug("loadfirstfragment : playlist not received for level:" + _level);
                }
                return 1;
            }

            }

            if (_pts_loading_in_progress == false) {
                if (last_seqnum == _levels[level].end_seqnum) {
                    // if last segment was last fragment of VOD playlist, notify last fragment loaded event, and return
                    if (_hls.type == HLSTypes.VOD) {
                        _hls.dispatchEvent(new HLSEvent(HLSEvent.LAST_VOD_FRAGMENT_LOADED));
