Fixed the problem of index overflow when no lesser level exist at the end.