Reformat code
Add exception handling for file saving