rewrote the code using vector-math