Loads of Code Formatting/Refactoring

Made a load of changes to fix some bugs and also to follow WP Coding Standards.