I don't think it will work properly. It looks like you're using ```level``` variable out the cycle, so in this place ```level``` stores the level of the latest processed item. Please correct me if I wrong.