move cleanup code to a place where it is executed