refactor and remove foldLeft and use max/sum