Fix some resource leak issues and remove unnecessary fflush() calls (fclose() does the job on its own).