fixed extra pending output issue
need valrgind check again