do some refactoring with before_request()