improved buffer handling