Improve redirect code