Rework of the reordering of functions/globals

Not finished yet, but the foundation is good