modified for non empty buffer