Improve BufferImpl and tests

Improve BufferImpl and tests. Make other minor improvements.