added a bunch of error handling.