added out.flush() to some places here and there