replaced for loop with max() function