Added some error checking