Added error checks.
Script attempts to exit gracefully if there is a problem.