Updated buffer write logic