Fix a bug with buffer replacement