Added missing error checking and handling.