Updated and improved logic for dealing with errors better.