Error checking and minor fixes