Added some more error checking.