If this were a method you added to Array you could do away with some of the checks.