Extract and add error handling to file renaming