Added some error handeling and stuff