Fix buffer and error handling.