Added some more error checkings