added some preprocessor optimization and error detection