Added error handling in key areas.