Refactor min and max to use foldl.