switched for loops for more efficient while loops. made code cleaner with math.max