Improve stdbuf implementation