surely there is a way of abstracting away the max/min function instead of rewriting it every time.