Small performance improvement by not checking unnecessarily large numbers in the inner loop