added some extra error checking