proper calculation of buffer length