clean up and error catching