Refactor navigation generation to be functional

This makes it a lot less error prone and far more robust. As well as a
lot easier to understand