Fixed stupid mistakes, added error check