Code completely refactored, and tested for cases from zero up to but not including one quadrillion.