Using foreach instead of Aggregate in algorithm

Changed the counting algo to use foreach, because calling recursive
calls from lambda is probably a bad idea