finished mean function