Refactored login redirect script