Remove unneeded code from middleware function