Updated to use more efficient for loop