Defective solution. Sort of works, but something strange happens to the buffer.