improved code for redirection