(1) you intermixed use of the variable %power as a local and global variable is very strange and may be causing some problems, I cant see that you need to be using SET to create a global value so you should just use var %power = each time (the var is optional after the first time, i always leave it in just incase)
(2) this line has an extra } if (%power == $null) { var %power = 0 }}
(3) is there ment to be a ) on the 5) on this line if (%gexp <= 0) { var %gexp = 5) }
(4) there maybe other things but those were the first ones that stuck out to me.