Make it work for int64.
Bugfixes and cleanups.  Logic is improved.