Fixed bugs in output buffer size calculation