Added some error checking conditions