add error handling while writing to file