I cannot see any error in the code you provided.
In case you need 3level menu, you need to write css classes. An inspiration you can find here:css style
It is only for two levels.
In properties of your web part you need to define property CSS prefix
. For example you can set it to: top;second;third
Now if you check the code, each level will have the class in its name. Based on it you can write a css definition for each level.
You also need to check options Render CSS classes and Apply menu design.