clean up and get middleware working