At first glance, and without testing anything, the code you’ve got should work but I expect that you have a number of spurious </ul>
in your markup. You need to do nearly the same thing for the end_lvl
method.
function end_lvl(&$output, $depth) {
if($depth >= 2) {
$output .= '';
} else {
$output .= "</ul>";
}
}
I don’t know if that is the problem with your megamenu or not.