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.
Why is this a comment in the code. 
Also, this isn't only for airlocks. Other things can be emagged too.