Making write() call more efficient