Added redirect support and refactored