Code cleanup. Added exception catching.