Fixed boundary check of buffer.

And added buffer:Clear()