small cleanups, use Math.min rather than self-rolled