Replaced for loop with numpy code