bugfix: mis-chaining caused by integer overflow

I really need to rewrite kbtree some time.