support infinite number of submenus

Refactored template code to be more easily readable and support an infinite number of submenus instead of just two dimensions