Code cleanup and exception catching.