clean up and error check