middleware cleanup