function ctc_sanitize( $instance ) { // prefix in case WP core adds method with same name

		global $allowedposttags;

		// Array to add sanitized values to
		$sanitized_instance = array();

		// Loop valid fields to sanitize
		$fields = $this->ctc_prepared_fields();
		foreach ( $fields as $id => $field ) {

			// Get posted value
			$input = isset( $instance[$id] ) ? $instance[$id] : '';

			// General sanitization
			$output = trim( stripslashes( $input ) );

			// Sanitize based on type
			switch ( $field['type'] ) {

				// Text
				// Textarea
				case 'text':
				case 'textarea':

					// Strip tags if config does not allow HTML
					if ( empty( $field['allow_html'] ) ) {
						$output = trim( strip_tags( $output ) );
					}

					// Sanitize HTML in case used (remove evil tags like script, iframe) - same as post content
					$output = stripslashes( wp_filter_post_kses( addslashes( $output ), $allowedposttags ) );

					break;

				// Checkbox
				case 'checkbox':

					$output = ! empty( $output ) ? '1' : '';

					break;

				// Radio
				// Select
				case 'radio':
				case 'select':

					// If option invalid, blank it so default will be used
					if ( ! isset( $field['options'][$output] ) ) {
						$output = '';
					}

					break;

				// Number
				case 'number':

					// Force number
					$output = (int) $output;

					// Enforce minimum value
					$min = isset( $field['number_min'] ) && '' !== $field['number_min'] ? (int) $field['number_min'] : ''; // force number if set
					if ( '' !== $min && $output < $min ) { // allow 0, don't process if no value given ('')
						$output = $min;
					}

					// Enforce maximum value
					$max = isset( $field['number_max'] ) && '' !== $field['number_max'] ? (int) $field['number_max'] : ''; // force number if set
					if ( '' !== $max && $output > $max ) { // allow 0, don't process if no value given ('')
						$output = $max;
					}

					break;

				// URL
				case 'url':

					$output = esc_url_raw( $output ); // force valid URL or use nothing

					break;

				// Image
				case 'image':

					// Sanitize attachment ID
					$output = absint( $output );

					// Set empty if value is 0, attachment does not exist, or is not an image
					if ( empty( $output ) || ! wp_get_attachment_image_src( $output ) ) {
						$output = '';
					}

					break;

			}

			// Run additional custom sanitization function if field config requires it
			if ( ! empty( $field['custom_sanitize'] ) ) {
				$output = call_user_func( $field['custom_sanitize'], $output );
			}

			// Sanitization left value empty, use default if empty not allowed
			$output = trim( $output );
			if ( empty( $output ) && ! empty( $field['default'] ) && ! empty( $field['no_empty'] ) ) {
				$output = $field['default'];
			}

			// Add output to instance array
			$sanitized_instance[$id] = $output;

		}

		// Return for saving
		return $sanitized_instance;

	}