fix bug triggered by modal windows during login... this also clarifies some variable names and restructures the code to be more readable