Max function improved