Fixed some lines and add another error check