Improved handling of login tokens, potential solution for #199
Fixing a bug in last commit. Also fixing #200 and #201