please make all the flushes at the end of your code , there is no need for duplicating flush()