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