Added some extra error checking.