Simplify redirect code and make it easier to read