Added a bunch of error handling