Avoid integer overflow when dealing with very large systems

This is closely related to #495.  That one didn't entirely fix the problem.