Added more error checking in places where I've encountered errors.