Compute mid value correctly inside while loop.