Compute optimum delay time

This version of the patch is complete.  It works well under most circumstances.