added some error trapping