bug fix for writing output