added fflush(stdout) before close