Updated to function as express middleware