Fixed bug that allowed reading past the end of buffer.