Refactor some repeating code in before hooks