Rearrange code & clean up extra debug messages
Clean up SQL queries and use $wpdb->prepare() + variables (when possible)