corrected bug about writting output file