Added error checking and handling.