You could convert this into middleware instead of a separate function