Restored cleanup routines into each function