Add missing includes and preprocessor checks