Minor fixes for putting untested code to use.
fixed buffer management