Improved automatic document detection