added wait time optimization. didn't get a chance to check if it works
and didn't sort out the potential issues with "distances" versus "times"
yet. also may have a crash condition that needs fixing.