Improve redirect function