added some error checking and changed printing slightly