Clean up redirect function a bit