Added a lot of errorchecking