Improved automatic document detection
