Changed some file writing function to improve software performance