refactored code to work in place on the buffers