The problem is not so much to do with your script but an issue where you are attempting to change your usermode prior to authentication of your name. You merely need slow down the usermode change by a second (or 5) to ensure you have authenticated correctly prior to attempting to change your usermode. Change the usermode script to a once-off timer and see how it goes.