File restructure

No functional changes. Just moving the declarations of functions around.
I may be wrong in this, IE I don't know if declaration order matters in this, if it does, just reject my PR. Otherwise accepting it, would be good to help newbie coders to understand code flow a little easier.
This is a actual issue discussed on irc every so often but nothing has been done so far since lot needs fixing.