Broke the main redirect function into smaller functions
Updated the related tests