WIP: now retries upload every 5 secs. Fixed bugs.

Still quite lame.  The binary search is pretty stupid, and still results in sequential scans behind the scenes. Will improve later.