A bit more error checking and handling