adding error handling to protect data from corruption.