Improve error handling
reformat code.