Code refactored from https://github.com/DLu/navigation/commit/78d47bb35407038053c5f92cfffd33ab70cb78ec
Bunch of formatting fixes