Added some error checking and handling.