Rewrote the core menu building algorithm

The algorithm is BFS-based now since it provides better workability of
the tree structure.

To-do: Positioning