Working version with BigInteger.
Improved version of the original - better speed.