Fix buffer overrun problem.
Clear out buffer in proper places.
Increased performance.
Code cosmetics.
Fixed minor casting problem to prevent complier warning.