Fixed handling of EOF and short output buffers