Fixed error in writing to file.