creating a context method and updating middleware to ensure the context is set properly