Handle redirect logic better