use iterative binary search type algorithm for determining optimal size

Pretty much same as before, just quicker.