Fixed bugs and handled exceptions