Replaced Foreach with Count loop