Added some error detections.