added optimal size calculation