refactoring redirect function