Trying to make error checking code more efficient