just moved a couple things around and added a little bit of error catching