Fixed all includes, global variables and functions

Grouped together from all over the file
Rearranged by category
Sorted alphabetically
Fixed capitalization
Removed duplicates