Added some better error handlings