fix infinite redirect bug and clean up some code (bug 900892)
Merge pull request #287 from wraithan/redirect

fix infinite redirect bug and clean up some code (bug 900892)