Script gets stuck on windows

So I created this script to authenticate users, set auto log out timer, and transfer them to appropriate page. And 75% of the time it works perfectly. But every so often when clicking on pages it seems to stay on the current page and not swap to the next one.

any ideas why ?

thank you in advance, code is below.

# This script controls the badge access feature
# It allows the user to log into a page, or 
# redirects them to the invalid login, or access denied page
# it also resets and sets the timer to log out user after set time 
                                          
#Displays keyboard to scan badge or enter emergency use password
                                          
password = system.gui.showTouchscreenKeyboard("Scan Badge or Enter Emergency Password")
                                          
# opens and scans user table to cross reference password to username to provide login credentials.
system.nav.openWindow("Main Windows/TableImport")
window = system.gui.getWindow("Main Windows/TableImport")
data = window.rootContainer.getComponent("User Table").data
system.nav.closeWindow("Main Windows/TableImport")
user_name = ""

for row in range(data.rowCount):
	index_row = data.getValueAt(row, 3)
	if index_row == password:
		user_name = data.getValueAt(row, 1)
		break

# Allows successful access to page if user role has security clearance, enable and resets timer
# sends them to access denied page if user does not have valid security clearance
success = system.security.switchUser(user_name, password)
roles = system.security.getRoles()
if u'Administrator' in roles or u'LVL_3' in roles or u'LVL_4' in roles:
	system.nav.swapTo('T_40GPM')
	system.tag.writeToTag("[Client]BadgeSystem/TimerValue", 0)
	system.tag.writeToTag("[Client]BadgeSystem/DisplayCountdown", 1)
	system.tag.writeToTag("[Client]BadgeSystem/TimerRunning", 0)
else:
	system.nav.swapTo('Access Denied')
	system.tag.writeToTag("[Client]BadgeSystem/TimerValue", 0)
	system.tag.writeToTag("[Client]BadgeSystem/DisplayCountdown", 0)
	system.tag.writeToTag("[Client]BadgeSystem/TimerRunning", 0)
    
# Sends user to failed login page if badge scan does not work
                                                                                                                                                             
if not success:
	system.nav.swapTo('Failed Login')
	system.tag.writeToTag("[Client]BadgeSystem/TimerValue", 0)
	system.tag.writeToTag("[Client]BadgeSystem/DisplayCountdown", 0)
	system.tag.writeToTag("[Client]BadgeSystem/TimerRunning", 0)

I think they must have due to multiple windows opening on startup. will update if issues persist.