moved cleanup code into macro