Fixed bug with remaining characters returning more than once and incorrect
buffer clearing.