Updated before_request function