I forgot to use var in math-function so some locals were actually clobbering globals