added more careful error checking when sending data