move some code back in buffer.c