Rewrite totalmax calculation