Add on error and close functions