Add missing includes and tidy up
Clean up and restructure sources
Remove unused macros, config variables, and file