Added a few error handlings, minor makeovers